Skip to content

Commit

Permalink
Merge pull request #41 from xylix/dev/macos-ask-for-permission
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Jul 19, 2020
2 parents 8075f16 + 724a3e2 commit 48491bc
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
32 changes: 30 additions & 2 deletions aw_watcher_window/macos.py
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:
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
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
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
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

0 comments on commit 48491bc

Please sign in to comment.