diff --git a/.gitignore b/.gitignore index f13f69ce..99a8446f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__ build dist *.swp +.mypy_cache diff --git a/README.md b/README.md index 2a420a49..27e813dc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -aw-watcher-window -================= +# aw-watcher-window Cross-platform window-Watcher for Linux (X11), macOS, Windows. @@ -7,6 +6,5 @@ Cross-platform window-Watcher for Linux (X11), macOS, Windows. ### Note to macOS users -To log current window title the teminal needs access to macOS accessibility API. +To log current window title the terminal needs access to macOS accessibility API. This can be enabled in System Preferences > Security & Privacy > Accessibility, then add the Terminal to this list. If this is not enabled the watcher can only log current application, and not window title. - diff --git a/aw-watcher-window.spec b/aw-watcher-window.spec index be81152e..8793f0af 100644 --- a/aw-watcher-window.spec +++ b/aw-watcher-window.spec @@ -6,7 +6,7 @@ block_cipher = None a = Analysis(['aw_watcher_window/__main__.py'], pathex=[], binaries=None, - datas=[("aw_watcher_window/printAppTitle.scpt", "aw_watcher_window")], + datas=[], hiddenimports=[], hookspath=[], runtime_hooks=[], diff --git a/aw_watcher_window/lib.py b/aw_watcher_window/lib.py index 010fb802..b0c42736 100644 --- a/aw_watcher_window/lib.py +++ b/aw_watcher_window/lib.py @@ -18,11 +18,7 @@ def get_current_window_linux() -> Optional[dict]: def get_current_window_macos() -> Optional[dict]: from . import macos - info = macos.getInfo() - app = macos.getApp(info) - title = macos.getTitle(info) - - return {"title": title, "appname": app} + return macos.getInfo() def get_current_window_windows() -> Optional[dict]: diff --git a/aw_watcher_window/macos.py b/aw_watcher_window/macos.py index 9cc04d9a..46dda4d2 100644 --- a/aw_watcher_window/macos.py +++ b/aw_watcher_window/macos.py @@ -1,17 +1,28 @@ -import subprocess -from subprocess import PIPE -import os +from typing import Optional +from AppKit import NSWorkspace +from Quartz import ( + CGWindowListCopyWindowInfo, + kCGWindowListOptionOnScreenOnly, + kCGNullWindowID +) +def getInfo() -> Optional[dict]: + app_name = '' + title = '' -def getInfo() -> str: - cmd = ["osascript", os.path.join(os.path.dirname(os.path.realpath(__file__)), "printAppTitle.scpt")] - p = subprocess.run(cmd, stdout=PIPE) - return str(p.stdout, "utf8").strip() + app = NSWorkspace.sharedWorkspace().activeApplication() + if app: + pid = app['NSApplicationProcessIdentifier'] -def getApp(info) -> str: - return info.split('","')[0][1:] + options = kCGWindowListOptionOnScreenOnly + window_list = CGWindowListCopyWindowInfo(options, kCGNullWindowID) + for window in window_list: + if pid == window['kCGWindowOwnerPID']: + # We could use app['NSApplicationName'], but this value is more + # accurate and matches other methods (like applescript) + app_name = window['kCGWindowOwnerName'] + title = window.get('kCGWindowName', u'') + break - -def getTitle(info) -> str: - return info.split('","')[1][:-1] + return {"appname": app_name, "title": title} diff --git a/aw_watcher_window/printAppTitle.scpt b/aw_watcher_window/printAppTitle.scpt deleted file mode 100644 index d004b036..00000000 Binary files a/aw_watcher_window/printAppTitle.scpt and /dev/null differ