Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript syntax to automate Xcode 4.1 to Clean, Build then Run

I appreciate that there are already questions on this topic, but having read the ones I can find (particularly this one: Tell AppleScript To Build XCode Project), they all seem to be a couple of years old and the answers do not seem to apply to the current version of Xcode.

Similarly to the linked question, I am attempting to automate opening an Xcode project, building it and then running the app in the iPhone Simulator (v4.3). The project in question is the Selenium project's iPhoneDriver (see here for details: http://code.google.com/p/selenium/wiki/IPhoneDriver)

Based on the answer in the other question, I have written the following script:

tell application "Xcode"
    open "/Users/<username>/Documents/Code/Selenium/iphone/iWebDriver.xcodeproj"
    tell project "iWebDriver"
         clean
         build
         try
             debug
         end try
    end tell
end tell

Unfortunately, when I run this I get the following:

tell application "Xcode"
open "/Users/ben.adderson/Documents/Code/Selenium/iphone/iWebDriver.xcodeproj"
    --> project document "iWebDriver.xcodeproj"
clean project "iWebDriver"
    --> missing value
build project "iWebDriver"
    --> missing value
debug project "iWebDriver"
    --> error number -1708
end tell

If I run just the open command, Xcode opens the project without issue. But as soon as I include the rest of the script the Xcode icon in the dock bounces, but that's all I get, apart from the above from the AppleScript Editor.

Can anybody advise what I'm doing wrong? This is the first time I've used AppleScript or Xcode, so I'm struggling to diagnose the problem.

I've tried looking at the Xcode AppleScript Dictionary, but without worked examples I can't quite determine the syntax I need.

Thanks in advance for any help!

like image 318
BenA Avatar asked Sep 02 '11 20:09

BenA


1 Answers

Using a mix of AppleScript, and command line tool (xcodebuild), I came up with this:

-- This script will clean, build then run the project at the path below.
-- You will need to slightly modify this script if you have more than one xcodeproject at this path

set pathOfXcodeProject to "/Users/<username>/Documents/Code/Selenium/iphone/iWebDriver.xcodeproj"
-- End of configuration



do shell script "cd " & pathOfXcodeProject & " && /usr/bin/xcodebuild clean build "

tell application "Xcode"

    open pathOfXcodeProject
    activate
end tell

tell application "System Events"
    get system attribute "sysv"
    if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
        if UI elements enabled then
            tell application process "Xcode"
                click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1

            end tell
        else
            beep
            display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again." with icon stop
            if button returned of result is "OK" then
                tell application "System Preferences"
                    activate
                    set current pane to pane "com.apple.preference.universalaccess"
                end tell
            end if
        end if
    else
        beep
        display dialog "This computer cannot run this script" & return & return & "The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer." with icon caution buttons {"Quit"} default button "Quit"
    end if
end tell

Let me know if this works for you. I tested it against Xcode 4.2.1 on Lion, with an iPhone project.

like image 109
Guillaume Avatar answered Oct 12 '22 23:10

Guillaume