get-frontwin-title (1254B)
1 #!/usr/bin/env bash 2 # on macOS, gets the title of the frontmost window. 3 if ! command -v osascript 1>/dev/null 2>&1; then 4 echo "osascript not installed." >&2 5 exit 1 6 fi 7 # Either use first arg as app name 8 if [ $# -ge 1 ]; then 9 osascript <<YEET 10 tell application "$1" 11 activate 12 end tell 13 14 tell application "System Events" 15 # Get the frontmost app's *process* object. 16 set frontAppProcess to first application process whose frontmost is true 17 end tell 18 19 # Tell the *process* to count its windows and return its front window's name. 20 tell frontAppProcess 21 if (count of windows) > 0 then 22 set window_name to name of front window 23 end if 24 end tell 25 YEET 26 # Or just use currently focused app 27 else 28 echo "No args passed, getting title of frontmost window of current application." 29 osascript <<YEET 30 tell application "System Events" 31 # Get the frontmost app's *process* object. 32 set frontAppProcess to first application process whose frontmost is true 33 end tell 34 35 # Tell the *process* to count its windows and return its front window's name. 36 tell frontAppProcess 37 if (count of windows) > 0 then 38 set window_name to name of front window 39 end if 40 end tell 41 YEET 42 fi