Skip to content

Instantly share code, notes, and snippets.

@mhaylock
Last active January 3, 2016 22:29
Show Gist options
  • Save mhaylock/8528394 to your computer and use it in GitHub Desktop.
Save mhaylock/8528394 to your computer and use it in GitHub Desktop.
AppleScript to tell Xcode to run app on multiple devices at once. Use here to run on "Example’s iPad" and "iPhone". For some reason Xcode crashes if I try to choose the iOS version in the simulator as well, i.e. `menu_click({"Xcode", "Product", "Destination", "iPhone"})` will work fine, but `menu_click({"Xcode", "Product", "Destination", "iPhone…
---- Handy menu_click code from http://hints.macworld.com/article.php?story=20060921045743404:
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
---- End of menu_click code
-- Make sure Xcode is the active application:
tell application "Xcode"
activate
end tell
-- This debug code is useful for discovering what AppleScript sees as the contents of particular menus:
tell application "System Events"
tell process "Xcode"
set destinationMenu to name of every menu item of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
set iPhoneMenu to name of every menu item of menu 1 of menu item "iPhone" of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
log "Destination Menu: "
log destinationMenu
log "iPhone Menu: "
log iPhoneMenu
end tell
end tell
-- Now the code to actually instruct Xcode to do what we want:
menu_click({"Xcode", "Product", "Destination", "iPhone"})
menu_click({"Xcode", "Product", "Run"})
delay 2 -- Give enough time for first build to complete and deploy to the simulator (will depend on the speed of your machine)
menu_click({"Xcode", "Product", "Destination", "Example’s iPad"})
menu_click({"Xcode", "Product", "Run"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment