@@ -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,40 +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 ( )
126+
127+ if UserDefaults . shared. value ( for: \. triggerActionWithAccessibilityAPI) {
128+ if !activeXcode. isActive { activeXcode. activate ( ) }
129+ let app = AXUIElementCreateApplication ( activeXcode. processIdentifier)
123130
124- let app = AXUIElementCreateApplication ( activeXcode. processIdentifier)
125- if let editorMenu = app. menuBar? . child ( title: " Editor " ) ,
126- let commandMenu = editorMenu. child ( title: bundleName) ,
127- let button = commandMenu. child ( title: name, description: " menu bar item " )
128- {
129- AXUIElementPerformAction ( button, " press " as CFString )
130- } else if let commandMenu = app. menuBar? . child ( title: bundleName) ,
131- let button = commandMenu. child ( title: name, description: " menu bar item " )
132- {
133- AXUIElementPerformAction ( button, " press " as CFString )
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+ }
134179 }
135-
136- // /// check if menu is open, if not, click the menu item.
137- // let appleScript = """
138- // tell application "System Events"
139- // set theprocs to every process whose unix id is \(activeXcode.processIdentifier)
140- // repeat with proc in theprocs
141- // set the frontmost of proc to true
142- // tell proc
143- // repeat with theMenu in menus of menu bar 1
144- // set theValue to value of attribute "AXVisibleChildren" of theMenu
145- // if theValue is not {} then
146- // return
147- // end if
148- // end repeat
149- // click menu item "\(name)" of menu 1 of menu item "\(bundleName)" of menu 1 of
150- // menu bar item "Editor" of menu bar 1
151- // end tell
152- // end repeat
153- // end tell
154- // """
155- //
156- // try await runAppleScript(appleScript)
157180 }
158181
159182 public static var makeXcodeActive : ( ) async throws -> Void = {
0 commit comments