Skip to content


fix: improved applescript strategy (cleartext src, precompile, run in…
Browse files Browse the repository at this point in the history
…-process) (#56)
  • Loading branch information
ErikBjare committed Jun 15, 2021
1 parent 71daa0f commit 8682633
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 121 deletions.
1 change: 0 additions & 1 deletion aw-watcher-window.spec
Expand Up @@ -8,7 +8,6 @@ a = Analysis(
("aw_watcher_window/printAppTitle.scpt", "aw_watcher_window"),
("aw_watcher_window/printAppStatus.jxa", "aw_watcher_window"),
Expand Down
56 changes: 45 additions & 11 deletions aw_watcher_window/
Expand Up @@ -2,30 +2,64 @@
import subprocess
from subprocess import PIPE
from typing import Dict

from Foundation import NSAppleScript

# the applescript version of the macos strategy is kept here until the jxa
# approach is proven out in production environments

source = """
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end try
end tell
end tell
return frontAppName & "
" & windowTitle

script = None

def getInfo() -> Dict[str, str]:
cmd = [
os.path.join(os.path.dirname(os.path.realpath(__file__)), "printAppTitle.scpt"),
p =, stdout=PIPE)
info = str(p.stdout, "utf8").strip()
# Cache compiled script
global script
if script is None:
script = NSAppleScript.alloc().initWithSource_(source)

app = getApp(info)
title = getTitle(info)
# Call script
result, errorinfo = script.executeAndReturnError_(None)
if errorinfo:
raise Exception(errorinfo)
output = result.stringValue()

# Ensure there's no extra newlines in the output
assert len(output.split("\n")) == 2

app = getApp(output)
title = getTitle(output)

return {"app": app, "title": title}

def getApp(info: str) -> str:
return info.split('","')[0][1:]
return info.split('\n')[0]

def getTitle(info: str) -> str:
return info.split('","')[1][:-1]
return info.split('\n')[1]

if __name__ == "__main__":
info = getInfo()
Binary file removed aw_watcher_window/printAppTitle.scpt
Binary file not shown.

0 comments on commit 8682633

Please sign in to comment.