Skip to content

Commit 48491bc

Browse files
authored
Merge pull request #41 from xylix/dev/macos-ask-for-permission
2 parents 8075f16 + 724a3e2 commit 48491bc

4 files changed

Lines changed: 130 additions & 3 deletions

File tree

‎aw_watcher_window/macos.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,37 @@ def getInfo() -> str:
99
return str(p.stdout, "utf8").strip()
1010

1111

12-
def getApp(info) -> str:
12+
def getApp(info: str) -> str:
1313
return info.split('","')[0][1:]
1414

1515

16-
def getTitle(info) -> str:
16+
def getTitle(info: str) -> str:
1717
return info.split('","')[1][:-1]
18+
19+
20+
def background_ensure_permissions() -> None:
21+
from multiprocessing import Process
22+
permission_process = Process(target=ensure_permissions, args=(()))
23+
permission_process.start()
24+
return
25+
26+
27+
def ensure_permissions() -> None:
28+
from ApplicationServices import AXIsProcessTrusted
29+
from AppKit import NSAlert, NSAlertFirstButtonReturn, NSWorkspace, NSURL
30+
accessibility_permissions = AXIsProcessTrusted()
31+
if not accessibility_permissions:
32+
title = "Missing accessibility permissions"
33+
info = "To let ActivityWatch capture window titles grant it accessibility permissions. \n If you've already given ActivityWatch accessibility permissions and are still seeing this dialog, try removing and re-adding them."
34+
35+
alert = NSAlert.new()
36+
alert.setMessageText_(title)
37+
alert.setInformativeText_(info)
38+
39+
ok_button = alert.addButtonWithTitle_("Open accessibility settings")
40+
41+
alert.addButtonWithTitle_("Close")
42+
choice = alert.runModal()
43+
print(choice)
44+
if choice == NSAlertFirstButtonReturn:
45+
NSWorkspace.sharedWorkspace().openURL_(NSURL.URLWithString_("x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"))

‎aw_watcher_window/main.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def main():
3030
setup_logging(name="aw-watcher-window", testing=args.testing, verbose=args.verbose,
3131
log_stderr=True, log_file=True)
3232

33+
if sys.platform == "darwin":
34+
from . import macos
35+
macos.background_ensure_permissions()
36+
3337
client = ActivityWatchClient("aw-watcher-window", testing=args.testing)
3438

3539
bucket_id = "{}_{}".format(client.client_name, client.client_hostname)
@@ -38,6 +42,7 @@ def main():
3842
client.create_bucket(bucket_id, event_type, queued=True)
3943

4044
logger.info("aw-watcher-window started")
45+
4146
sleep(1) # wait for server to start
4247
with client:
4348
heartbeat_loop(client, bucket_id, poll_time=args.poll_time, exclude_title=args.exclude_title)

‎poetry.lock‎

Lines changed: 92 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ aw-client = {git = "https://github.com/ActivityWatch/aw-client.git"}
1414
python-xlib = {version = "^0.26", platform = "linux"}
1515
pypiwin32 = {version = "223", platform = "win32"}
1616
wmi = {version = "^1.4.9", platform = "win32"}
17+
pyobjc-framework-ApplicationServices = { version = "^6.2", platform="darwin"}
18+
pyobjc-framework-CoreText = {version = "^6.2", platform="darwin"}
19+
1720

1821
[tool.poetry.dev-dependencies]
1922
pytest = "^5.3.2"

0 commit comments

Comments
 (0)