From d058db651fbf22e29912c04259b38edfeec990a7 Mon Sep 17 00:00:00 2001 From: metacodes Date: Thu, 17 Mar 2022 17:33:41 +0800 Subject: [PATCH 1/3] fix issue #1249 #1079 #1392 I am able to reproduce the issue by reinstall JetBrains AppCode. The first time I open AppCode after reinstall it, AltTab never show AppCode window. I debug the code on my Mac, I find that AltTab can get the event of app launched but never get the event of finishedLaunching. So I just remove the code runningApplication.isFinishedLaunching. After that, AltTab work well and the issue is fixed. --- src/logic/Application.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 486cab080..933156e6b 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -72,7 +72,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") From 5d33d3a4ba3c3a9b9d06fa23f424daf1c2f697cf Mon Sep 17 00:00:00 2001 From: metacodes Date: Sun, 20 Mar 2022 11:21:12 +0800 Subject: [PATCH 2/3] fix: some app is active but can not get windows 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. At this scenario, AXUIElementCopyAttributeValue(self, kAXWindowsAttribute, &value) returns success but AXUIElementCopyAttributeValue(self, kAXPositionAttribute, &value) and AXUIElementCopyAttributeValue(self, kAXSizeAttribute, &value) return attributeUnsupported. After we wait for a moment, all things run well. --- src/logic/Application.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 933156e6b..8327297a0 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -117,6 +117,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 + } } } } From c68b298ab32ee9c7c6128ea4146d9d1a4a4ceffa Mon Sep 17 00:00:00 2001 From: metacodes Date: Tue, 29 Mar 2022 19:07:36 +0800 Subject: [PATCH 3/3] fix: the variable wasLaunchedBeforeAltTab is not initialized correctly --- src/logic/Application.swift | 3 ++- src/logic/Applications.swift | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/logic/Application.swift b/src/logic/Application.swift index 8327297a0..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 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)) } } }