Skip to main content

Add my script for “get frontmost window title”

ID
ec9b186
date
2023-12-25 23:42:32+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
e26d681
message
Add my script for "get frontmost window title"
changed files
3 files, 45 additions, 1 deletion

Changed files

.gitattributes (164) → .gitattributes (220)

diff --git a/.gitattributes b/.gitattributes
index 80ccdb3..1f07a11 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,4 +1,5 @@
 close_tabs linguist-language=JavaScript
 get_focus_mode linguist-language=JavaScript
+get_frontmost_window_title linguist-language=JavaScript
 get_live_text linguist-language=Swift
 set_accent_colour linguist-language=Swift

macos/README.md (4207) → macos/README.md (4757)

diff --git a/macos/README.md b/macos/README.md
index bc53eea..d01cbf4 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -37,6 +37,10 @@ scripts = [
         "description": "prints the current Focus mode"
     },
     {
+        "usage": "get_frontmost_window_title [APP_NAME]",
+        "description": "prints the title of the frontmost window in the named app. This is particularly useful when working with apps that don't support AppleScript."
+    },
+    {
         "name": "get_live_text [image]",
         "description": "get OCR'd text for a single image using Live Text"
     },
@@ -104,6 +108,14 @@ get OCR'd text for all the images in a directory using Live Text
 prints the current Focus mode
 </dd>
 <dt>
+<a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_frontmost_window_title">
+<code>get_frontmost_window_title [APP_NAME]</code>
+</a>
+</dt>
+<dd>
+prints the title of the frontmost window in the named app. This is particularly useful when working with apps that don't support AppleScript.
+</dd>
+<dt>
 <a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_live_text [image]">
 <code>get_live_text [image]</code>
 </a>
@@ -139,4 +151,4 @@ alias for <code>security unlock-keychain ~/Library/Keychains/login.keychain</cod
 </p>
 </dd>
 </dl>
-<!-- [[[end]]] (checksum: 7185b51098c3e95c9d1fc51f860b1dd2) -->
+<!-- [[[end]]] (checksum: ee1950858a05fd7ea7bd4e60e619d61f) -->

macos/get_frontmost_window_title (0) → macos/get_frontmost_window_title (859)

diff --git a/macos/get_frontmost_window_title b/macos/get_frontmost_window_title
new file mode 100755
index 0000000..14b21e2
--- /dev/null
+++ b/macos/get_frontmost_window_title
@@ -0,0 +1,31 @@
+#!/usr/bin/env osascript -l JavaScript
+// Given the name of an app, print the title of the frontmost window
+// in that app.
+//
+// Examples:
+//
+//      $ get_frontmost_window_title "Obsidian"
+//      Short story ideas - textfiles - Obsidian v1.4.16
+//
+//      $ get_frontmost_window_title "TextMate"
+//      get_frontmost_window_title — macos (git: main)
+//
+// This is particularly useful for writing automations for apps that
+// don't support AppleScript.
+
+function run(argv) {
+  if (argv.length !== 1) {
+    throw new Error("Usage: get_frontmost_window_title <APP_NAME>")
+  }
+
+  const appName = argv[0];
+
+  const systemEvents = Application("System Events");
+  const process = systemEvents.processes[appName];
+
+  if (process.windows.length > 0) {
+    return process.windows[0].name();
+  } else {
+    throw new Error(`No windows open in ${appName}!`);
+  }
+}