@@ -3,6 +3,7 @@ import AppKit
33import AXExtension
44import CopilotService
55import Foundation
6+ import Logger
67
78public struct NoAccessToAccessibilityAPIError : Error , LocalizedError {
89 public var errorDescription : String ? {
@@ -120,27 +121,62 @@ public enum Environment {
120121 else { return }
121122 let bundleName = Bundle . main
122123 . object ( forInfoDictionaryKey: " EXTENSION_BUNDLE_NAME " ) as! String
124+
125+ await Task . yield ( )
123126
124- /// check if menu is open, if not, click the menu item.
125- let appleScript = """
126- tell application " System Events "
127- set theprocs to every process whose unix id is \( activeXcode. processIdentifier)
128- repeat with proc in theprocs
129- set the frontmost of proc to true
130- tell proc
131- repeat with theMenu in menus of menu bar 1
132- set theValue to value of attribute " AXVisibleChildren " of theMenu
133- if theValue is not {} then
134- return
135- end if
136- end repeat
137- click menu item " \( name) " of menu 1 of menu item " \( bundleName) " of menu 1 of menu bar item " Editor " of menu bar 1
138- end tell
139- end repeat
140- end tell
141- """
127+ if UserDefaults . shared. value ( for: \. triggerActionWithAccessibilityAPI) {
128+ if !activeXcode. isActive { activeXcode. activate ( ) }
129+ let app = AXUIElementCreateApplication ( activeXcode. processIdentifier)
142130
143- try await runAppleScript ( appleScript)
131+ if let editorMenu = app. menuBar? . child ( title: " Editor " ) ,
132+ let commandMenu = editorMenu. child ( title: bundleName)
133+ {
134+ if let button = commandMenu. child ( title: name, role: " AXMenuItem " ) {
135+ let error = AXUIElementPerformAction ( button, kAXPressAction as CFString )
136+ if error != AXError . success {
137+ Logger . service
138+ . error ( " Trigger action \( name) failed: \( error. localizedDescription) " )
139+ throw error
140+ }
141+ }
142+ } else if let commandMenu = app. menuBar? . child ( title: bundleName) ,
143+ let button = commandMenu. child ( title: name, role: " AXMenuItem " )
144+ {
145+ let error = AXUIElementPerformAction ( button, kAXPressAction as CFString )
146+ if error != AXError . success {
147+ Logger . service
148+ . error ( " Trigger action \( name) failed: \( error. localizedDescription) " )
149+ throw error
150+ }
151+ }
152+ } else {
153+ /// check if menu is open, if not, click the menu item.
154+ let appleScript = """
155+ tell application " System Events "
156+ set theprocs to every process whose unix id is \( activeXcode. processIdentifier)
157+ repeat with proc in theprocs
158+ set the frontmost of proc to true
159+ tell proc
160+ repeat with theMenu in menus of menu bar 1
161+ set theValue to value of attribute " AXVisibleChildren " of theMenu
162+ if theValue is not {} then
163+ return
164+ end if
165+ end repeat
166+ click menu item " \( name) " of menu 1 of menu item " \( bundleName) " of menu 1 of menu bar item " Editor " of menu bar 1
167+ end tell
168+ end repeat
169+ end tell
170+ """
171+
172+ do {
173+ try await runAppleScript ( appleScript)
174+ } catch {
175+ Logger . service
176+ . error ( " Trigger action \( name) failed: \( error. localizedDescription) " )
177+ throw error
178+ }
179+ }
144180 }
145181
146182 public static var makeXcodeActive : ( ) async throws -> Void = {
0 commit comments