Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Ensure Accessibility permissions on macOS #41

Merged
merged 10 commits into from
Jul 19, 2020
32 changes: 30 additions & 2 deletions aw_watcher_window/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,37 @@ def getInfo() -> str:
return str(p.stdout, "utf8").strip()


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


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


def background_ensure_permissions() -> None:
from multiprocessing import Process
permission_process = Process(target=ensure_permissions, args=(()))
permission_process.start()
return


def ensure_permissions() -> None:
from ApplicationServices import AXIsProcessTrusted
from AppKit import NSAlert, NSAlertFirstButtonReturn, NSWorkspace, NSURL
accessibility_permissions = AXIsProcessTrusted()
if not accessibility_permissions:
xylix marked this conversation as resolved.
Show resolved Hide resolved
title = "Missing accessibility permissions"
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."

alert = NSAlert.new()
alert.setMessageText_(title)
alert.setInformativeText_(info)

ok_button = alert.addButtonWithTitle_("Open accessibility settings")

alert.addButtonWithTitle_("Close")
choice = alert.runModal()
print(choice)
if choice == NSAlertFirstButtonReturn:
NSWorkspace.sharedWorkspace().openURL_(NSURL.URLWithString_("x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"))
5 changes: 5 additions & 0 deletions aw_watcher_window/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def main():
setup_logging(name="aw-watcher-window", testing=args.testing, verbose=args.verbose,
log_stderr=True, log_file=True)

if sys.platform == "darwin":
from . import macos
xylix marked this conversation as resolved.
Show resolved Hide resolved
macos.background_ensure_permissions()

client = ActivityWatchClient("aw-watcher-window", testing=args.testing)

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

logger.info("aw-watcher-window started")

sleep(1) # wait for server to start
with client:
heartbeat_loop(client, bucket_id, poll_time=args.poll_time, exclude_title=args.exclude_title)
Expand Down
93 changes: 92 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ aw-client = {git = "https://github.com/ActivityWatch/aw-client.git"}
python-xlib = {version = "^0.26", platform = "linux"}
pypiwin32 = {version = "223", platform = "win32"}
wmi = {version = "^1.4.9", platform = "win32"}
pyobjc-framework-ApplicationServices = { version = "^6.2", platform="darwin"}
pyobjc-framework-CoreText = {version = "^6.2", platform="darwin"}


[tool.poetry.dev-dependencies]
pytest = "^5.3.2"
Expand Down