Skip to content

Commit 7ffb3fc

Browse files
committed
Try a side-effect-ful import approach to initializing macOS event loop
1 parent 01a5f18 commit 7ffb3fc

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

aw_watcher_window/lib.py

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

1919
def get_current_window_macos() -> Optional[dict]:
2020
from . import macos
21-
21+
# The side effectful import breaks thigns here
2222
app = macos.get_current_app()
2323
print ("appname" + macos.get_app_name(app) + ", title" + macos.get_app_title(app))
2424
return {"appname": macos.get_app_name(app), "title": macos.get_app_title(app)}

aw_watcher_window/macos.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from typing import Dict, Optional
2-
from AppKit import NSObject, NSWorkspace, NSRunningApplication, NSWorkspaceDidActivateApplicationNotification
1+
from threading import Thread
2+
from typing import Dict, Optional, NoReturn
3+
from AppKit import NSObject, NSNotification, NSWorkspace, NSRunningApplication, NSWorkspaceDidActivateApplicationNotification
34
from Quartz import (
45
CGWindowListCopyWindowInfo,
56
kCGWindowListOptionOnScreenOnly,
@@ -13,16 +14,18 @@ class Observer(NSObject):
1314

1415
def get_front_app(self) -> NSRunningApplication:
1516
return self.app
16-
def set_front_app_(self) -> NSRunningApplication:
17+
18+
def handle_(self, noti: NSNotification) -> None:
19+
self._set_front_app()
20+
def _set_front_app(self) -> None:
1721
self.app = NSWorkspace.sharedWorkspace().frontmostApplication()
1822

1923
observer = Observer.new()
2024
NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_(
21-
observer,
22-
"set_front_app:",
23-
NSWorkspaceDidActivateApplicationNotification,
24-
None)
25-
25+
observer,
26+
"handle:",
27+
NSWorkspaceDidActivateApplicationNotification,
28+
None)
2629
AppHelper.runConsoleEventLoop()
2730

2831
def get_current_app() -> NSRunningApplication:
@@ -41,7 +44,6 @@ def get_app_title(app: NSRunningApplication) -> str:
4144
for window in windowList:
4245
lookupPid = window['kCGWindowOwnerPID']
4346
if (lookupPid == pid):
44-
print(window)
4547
return window.get('kCGWindowName', 'Non-detected window title')
4648

4749
return "Couldn't find title by pid"

0 commit comments

Comments
 (0)