diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 486cab080..877ca09ea 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -37,8 +37,9 @@ class Application: NSObject { return n } - init(_ runningApplication: NSRunningApplication) { + init(_ runningApplication: NSRunningApplication, _ wasLaunchedBeforeAltTab: Bool = false) { self.runningApplication = runningApplication + self.wasLaunchedBeforeAltTab = wasLaunchedBeforeAltTab pid = runningApplication.processIdentifier super.init() isHidden = runningApplication.isHidden @@ -72,7 +73,7 @@ class Application: NSObject { } func addAndObserveWindows() { - if runningApplication.isFinishedLaunching && runningApplication.activationPolicy != .prohibited && axUiElement == nil { + if runningApplication.activationPolicy != .prohibited && axUiElement == nil { axUiElement = AXUIElementCreateApplication(pid) AXObserverCreate(pid, axObserverCallback, &axObserver) debugPrint("Adding app", pid ?? "nil", runningApplication.bundleIdentifier ?? "nil") @@ -117,6 +118,11 @@ class Application: NSObject { if group == nil && !self.wasLaunchedBeforeAltTab && CGSSpaceGetType(cgsMainConnectionId, Spaces.currentSpaceId) == .fullscreen { throw AxError.runtimeError } + // workaround: sometimes some apps are launched and are actived but OS returns the windows.count == 0. we wait for a moment + // for example: Bear.app + if group == nil && !self.wasLaunchedBeforeAltTab && self.runningApplication.isActive { + throw AxError.runtimeError + } } } } diff --git a/src/logic/Applications.swift b/src/logic/Applications.swift index 061c1d66d..c16e4415c 100644 --- a/src/logic/Applications.swift +++ b/src/logic/Applications.swift @@ -21,7 +21,7 @@ class Applications { } static func addInitialRunningApplications() { - addRunningApplications(NSWorkspace.shared.runningApplications) + addRunningApplications(NSWorkspace.shared.runningApplications, true) } static func addInitialRunningApplicationsWindows() { @@ -39,10 +39,10 @@ class Applications { } } - static func addRunningApplications(_ runningApps: [NSRunningApplication]) { + static func addRunningApplications(_ runningApps: [NSRunningApplication], _ wasLaunchedBeforeAltTab: Bool = false) { runningApps.forEach { if isActualApplication($0) { - Applications.list.append(Application($0)) + Applications.list.append(Application($0, wasLaunchedBeforeAltTab)) } } }