Skip to content

Commit aac4f88

Browse files
committed
Update MacOS title logic
- Switch from the opaque scpt file to a normal JS file using JavaScript for Automation. - Grab the first window (which is the active one) and get its name as the title. This worked across all apps I tried, including Sublime, Atom, Chrome, Safari, and iTerm - Clean up the api to the macos.py file (have it return the dict directly) - Fix a typo in README
1 parent eb0f791 commit aac4f88

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
build
55
dist
66
*.swp
7+
.mypy_cache

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
aw-watcher-window
2-
=================
1+
# aw-watcher-window
32

43
Cross-platform window-Watcher for Linux (X11), macOS, Windows.
54

65
[![Build Status](https://travis-ci.org/ActivityWatch/aw-watcher-window.svg?branch=master)](https://travis-ci.org/ActivityWatch/aw-watcher-window)
76

87
### Note to macOS users
98

10-
To log current window title the teminal needs access to macOS accessibility API.
9+
To log current window title the terminal needs access to macOS accessibility API.
1110
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.
12-

aw_watcher_window/lib.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ def get_current_window_linux() -> Optional[dict]:
1818

1919
def get_current_window_macos() -> Optional[dict]:
2020
from . import macos
21-
info = macos.getInfo()
22-
app = macos.getApp(info)
23-
title = macos.getTitle(info)
24-
25-
return {"title": title, "appname": app}
21+
return macos.getInfo()
2622

2723

2824
def get_current_window_windows() -> Optional[dict]:

aw_watcher_window/macos.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import subprocess
2-
from subprocess import PIPE
3-
import os
1+
from typing import Optional
2+
from AppKit import NSWorkspace
3+
from Quartz import (
4+
CGWindowListCopyWindowInfo,
5+
kCGWindowListOptionOnScreenOnly,
6+
kCGNullWindowID
7+
)
48

9+
def getInfo() -> Optional[dict]:
10+
app_name = ''
11+
title = ''
512

6-
def getInfo() -> str:
7-
cmd = ["osascript", os.path.join(os.path.dirname(os.path.realpath(__file__)), "printAppTitle.scpt")]
8-
p = subprocess.run(cmd, stdout=PIPE)
9-
return str(p.stdout, "utf8").strip()
13+
app = NSWorkspace.sharedWorkspace().activeApplication()
1014

15+
if app:
16+
pid = app['NSApplicationProcessIdentifier']
1117

12-
def getApp(info) -> str:
13-
return info.split('","')[0][1:]
18+
options = kCGWindowListOptionOnScreenOnly
19+
window_list = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
20+
for window in window_list:
21+
if pid == window['kCGWindowOwnerPID']:
22+
# We could use app['NSApplicationName'], but this value is more
23+
# accurate and matches other methods (like applescript)
24+
app_name = window['kCGWindowOwnerName']
25+
title = window.get('kCGWindowName', u'')
26+
break
1427

15-
16-
def getTitle(info) -> str:
17-
return info.split('","')[1][:-1]
28+
return {"appname": app_name, "title": title}
-3.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)