I have a simple script for programmatically launching an iOS project through XCode:
on run project_loc
tell application "Xcode"
open project_loc
activate
end tell
tell application "System Events"
tell application process "Xcode"
click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1
end tell
end tell
end run
This works well, except that it switches focus to Xcode, which isn’t ideal because I’m trying to use this as part of a scheme that automatically recompiles and relaunches in response to any change in relevant files. Is there any way to accomplish this without giving focus to the Xcode window?
You cannot use GUI Scripting to click a menu item without switching to the application any more than you can click it yourself without doing so – the menu bar of an application is only available when it has focus (or, at least, was the last app to have focus – it doesn’t necessarily need a focused window). However, you can alleviate the disruptiveness of the switch by switching back to your previously focused app inside your script:
But … Xcode itself is a scriptable application. If all you need is for the project to build, why not use
which will spare you the can of worms GUI Scripting is – have a look at the Xcode AppleScript dictionary (not added by default – just drag Xcode.app to the library window in AppleScript editor).