From 311db85844aceebb350345a02a8137dd5d568fad Mon Sep 17 00:00:00 2001 From: Leland Takamine Date: Fri, 28 Mar 2025 10:17:49 -0700 Subject: [PATCH] Run main loop before retrieving list of running applications on MacOS --- libs/computer-server/computer_server/handlers/macos.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/computer-server/computer_server/handlers/macos.py b/libs/computer-server/computer_server/handlers/macos.py index 024757fc..86efbe3b 100644 --- a/libs/computer-server/computer_server/handlers/macos.py +++ b/libs/computer-server/computer_server/handlers/macos.py @@ -352,7 +352,7 @@ class MacOSAccessibilityHandler(BaseAccessibilityHandler): """Get all visible windows in the system.""" try: windows = [] - running_apps = NSWorkspace.sharedWorkspace().runningApplications() + running_apps = self.get_running_apps() for app in running_apps: try: @@ -382,6 +382,13 @@ class MacOSAccessibilityHandler(BaseAccessibilityHandler): except: return [] + def get_running_apps(self): + # From NSWorkspace.runningApplications docs: https://developer.apple.com/documentation/appkit/nsworkspace/runningapplications + # "Similar to the NSRunningApplication class’s properties, this property will only change when the main run loop runs in a common mode" + # So we need to run the main run loop to get the latest running applications + Foundation.CFRunLoopRunInMode(Foundation.kCFRunLoopDefaultMode, 0.1, False) + return NSWorkspace.sharedWorkspace().runningApplications() + def get_ax_attribute(self, element, attribute): return element_attribute(element, attribute)