Skip to content

Commit 3d8e66e

Browse files
committed
Use Accessibility API to make an app active
1 parent 06e38e7 commit 3d8e66e

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

Tool/Sources/AXExtension/AXUIElement.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ public extension AXUIElement {
136136
}
137137

138138
var isFrontmost: Bool {
139-
(try? copyValue(key: kAXFrontmostAttribute)) ?? false
139+
get {
140+
(try? copyValue(key: kAXFrontmostAttribute)) ?? false
141+
}
142+
set {
143+
AXUIElementSetAttributeValue(
144+
self,
145+
kAXFrontmostAttribute as CFString,
146+
newValue as CFBoolean
147+
)
148+
}
140149
}
141150

142151
var focusedWindow: AXUIElement? {

Tool/Sources/AppActivator/AppActivator.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ public extension NSWorkspace {
1616
if activated { return }
1717

1818
// Fallback solution
19-
20-
let appleScript = """
21-
tell application "System Events"
22-
set frontmost of the first process whose unix id is \
23-
\(ProcessInfo.processInfo.processIdentifier) to true
24-
end tell
25-
"""
26-
try await runAppleScript(appleScript)
19+
20+
let axApplication = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier)
21+
axApplication.isFrontmost = true
22+
//
23+
// let appleScript = """
24+
// tell application "System Events"
25+
// set frontmost of the first process whose unix id is \
26+
// \(ProcessInfo.processInfo.processIdentifier) to true
27+
// end tell
28+
// """
29+
// try await runAppleScript(appleScript)
2730
}
2831
}
2932

@@ -32,15 +35,17 @@ public extension NSWorkspace {
3235
guard let app = await XcodeInspector.shared.safe.previousActiveApplication
3336
else { return }
3437
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
35-
_ = app.activate()
38+
app.appElement.isFrontmost = true
39+
// _ = app.activate()
3640
}
3741
}
3842

3943
static func activatePreviousActiveXcode(delay: TimeInterval = 0.2) {
4044
Task { @MainActor in
4145
guard let app = await XcodeInspector.shared.safe.latestActiveXcode else { return }
4246
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
43-
_ = app.activate()
47+
app.appElement.isFrontmost = true
48+
// _ = app.activate()
4449
}
4550
}
4651
}

Tool/Sources/ChatTab/ChatTab.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ open class BaseChatTab {
6767

6868
public init(store: StoreOf<ChatTabItem>) {
6969
chatTabStore = store
70-
self.id = store.id
71-
self.title = store.title
7270

7371
Task { @MainActor in
72+
self.title = store.title
73+
self.id = store.id
7474
storeObserver.observe { [weak self] in
7575
guard let self else { return }
7676
self.title = store.title

Tool/Sources/XcodeInspector/XcodeInspector+TriggerCommand.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ public extension AppInstanceInspector {
3030
guard path.count >= 2 else { throw cantRunCommand("Path too short.") }
3131

3232
if activateApp {
33-
if !runningApplication.activate() {
34-
Logger.service.error("""
35-
Trigger menu item \(sourcePath): \
36-
Xcode not activated.
37-
""")
38-
}
33+
appElement.isFrontmost = true
34+
// if !runningApplication.activate() {
35+
// Logger.service.error("""
36+
// Trigger menu item \(sourcePath): \
37+
// Xcode not activated.
38+
// """)
39+
// }
3940
} else {
4041
if !runningApplication.isActive {
4142
Logger.service.error("""

Version.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
APP_VERSION = 0.34.1
2-
APP_BUILD = 413
1+
APP_VERSION = 0.34.2
2+
APP_BUILD = 414
33

0 commit comments

Comments
 (0)