Skip to content

Commit c683671

Browse files
committed
fix: fixed import in main, moved macos permission prompt into seperate file
1 parent b8eb8e3 commit c683671

3 files changed

Lines changed: 68 additions & 46 deletions

File tree

aw_watcher_window/macos_jxa.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,6 @@ def getInfo() -> Dict[str, str]:
5454
return json.loads(result.stringValue())
5555

5656

57-
def background_ensure_permissions() -> None:
58-
from multiprocessing import Process
59-
60-
permission_process = Process(target=ensure_permissions, args=(()))
61-
permission_process.start()
62-
return
63-
64-
65-
def ensure_permissions() -> None:
66-
from ApplicationServices import AXIsProcessTrusted
67-
from AppKit import NSAlert, NSAlertFirstButtonReturn, NSWorkspace, NSURL
68-
69-
accessibility_permissions = AXIsProcessTrusted()
70-
if not accessibility_permissions:
71-
title = "Missing accessibility permissions"
72-
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."
73-
74-
alert = NSAlert.new()
75-
alert.setMessageText_(title)
76-
alert.setInformativeText_(info)
77-
78-
ok_button = alert.addButtonWithTitle_("Open accessibility settings")
79-
80-
alert.addButtonWithTitle_("Close")
81-
choice = alert.runModal()
82-
print(choice)
83-
if choice == NSAlertFirstButtonReturn:
84-
NSWorkspace.sharedWorkspace().openURL_(
85-
NSURL.URLWithString_(
86-
"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
87-
)
88-
)
89-
90-
9157
if __name__ == "__main__":
9258
print(getInfo())
9359
print("Waiting 5 seconds...")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)
4+
5+
6+
def background_ensure_permissions() -> None:
7+
from multiprocessing import Process
8+
9+
permission_process = Process(target=ensure_permissions, args=(()))
10+
permission_process.start()
11+
return
12+
13+
14+
def ensure_permissions() -> None:
15+
from ApplicationServices import AXIsProcessTrusted
16+
from AppKit import NSAlert, NSAlertFirstButtonReturn, NSWorkspace, NSURL
17+
18+
accessibility_permissions = AXIsProcessTrusted()
19+
if not accessibility_permissions:
20+
logger.info("No accessibility permissions, prompting user")
21+
title = "Missing accessibility permissions"
22+
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."
23+
24+
alert = NSAlert.new()
25+
alert.setMessageText_(title)
26+
alert.setInformativeText_(info)
27+
28+
ok_button = alert.addButtonWithTitle_("Open accessibility settings")
29+
30+
alert.addButtonWithTitle_("Close")
31+
choice = alert.runModal()
32+
if choice == NSAlertFirstButtonReturn:
33+
NSWorkspace.sharedWorkspace().openURL_(
34+
NSURL.URLWithString_(
35+
"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
36+
)
37+
)

aw_watcher_window/main.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,35 @@
1111

1212
from .lib import get_current_window
1313
from .config import parse_args
14+
from .macos_permissions import background_ensure_permissions
15+
1416

1517
logger = logging.getLogger(__name__)
1618

1719
# run with LOG_LEVEL=DEBUG
18-
log_level = os.environ.get('LOG_LEVEL')
20+
log_level = os.environ.get("LOG_LEVEL")
1921
if log_level:
2022
logger.setLevel(logging.__getattribute__(log_level.upper()))
2123

24+
2225
def main():
2326
args = parse_args()
2427

25-
if sys.platform.startswith("linux") and ("DISPLAY" not in os.environ or not os.environ["DISPLAY"]):
28+
if sys.platform.startswith("linux") and (
29+
"DISPLAY" not in os.environ or not os.environ["DISPLAY"]
30+
):
2631
raise Exception("DISPLAY environment variable not set")
2732

28-
setup_logging(name="aw-watcher-window", testing=args.testing, verbose=args.verbose,
29-
log_stderr=True, log_file=True)
33+
setup_logging(
34+
name="aw-watcher-window",
35+
testing=args.testing,
36+
verbose=args.verbose,
37+
log_stderr=True,
38+
log_file=True,
39+
)
3040

3141
if sys.platform == "darwin":
32-
from . import macos
33-
macos.background_ensure_permissions()
42+
background_ensure_permissions()
3443

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

@@ -43,7 +52,14 @@ def main():
4352

4453
sleep(1) # wait for server to start
4554
with client:
46-
heartbeat_loop(client, bucket_id, poll_time=args.poll_time, strategy=args.strategy, exclude_title=args.exclude_title)
55+
heartbeat_loop(
56+
client,
57+
bucket_id,
58+
poll_time=args.poll_time,
59+
strategy=args.strategy,
60+
exclude_title=args.exclude_title,
61+
)
62+
4763

4864
def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False):
4965
while True:
@@ -55,23 +71,26 @@ def heartbeat_loop(client, bucket_id, poll_time, strategy, exclude_title=False):
5571
current_window = get_current_window(strategy)
5672
logger.debug(current_window)
5773
except Exception as e:
58-
logger.error("Exception thrown while trying to get active window: {}".format(e))
74+
logger.error(
75+
"Exception thrown while trying to get active window: {}".format(e)
76+
)
5977
traceback.print_exc()
6078
current_window = {"app": "unknown", "title": "unknown"}
6179

6280
now = datetime.now(timezone.utc)
6381
if current_window is None:
64-
logger.debug('Unable to fetch window, trying again on next poll')
82+
logger.debug("Unable to fetch window, trying again on next poll")
6583
else:
6684
if exclude_title:
67-
current_window["title"] = "excluded"
85+
current_window["title"] = "excluded"
6886

6987
current_window_event = Event(timestamp=now, data=current_window)
7088

7189
# Set pulsetime to 1 second more than the poll_time
7290
# This since the loop takes more time than poll_time
7391
# due to sleep(poll_time).
74-
client.heartbeat(bucket_id, current_window_event,
75-
pulsetime=poll_time + 1.0, queued=True)
92+
client.heartbeat(
93+
bucket_id, current_window_event, pulsetime=poll_time + 1.0, queued=True
94+
)
7695

7796
sleep(poll_time)

0 commit comments

Comments
 (0)