BenA
BenA

Reputation: 2051

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!

Upvotes: 10

Views: 4466

Answers (3)

Motti Shneor
Motti Shneor

Reputation: 2194

Although I vote and cheer the above AppleScript, which is really good (and also works…) being all professional, taking care of old versions, and extreme cases etc. The script suggested by the question should have worked in the first place.

The reason it doesn't work is broken Xcode scriptability. Not only "clean" doesn't work. Most everything I tried to do with the internal object model of Xcode doesn't work.

AppleScript architecture (OSA) requires an application to expose a "human understandable" object graph that AppleScripts can reference. In Xcode it may look like:

tell target "my library" of project "my project" of workspace "my workspace" to clean.

or

tell application "Xcode" to close all projects whose name contains "Lib"

User-Interface scripting is the last resort of the scripter when application scriptability is poor. Because the UI hierarchy is standard in nature, and its scriptability is free from Cocoa, it most always work.

Upvotes: 0

psimekjr
psimekjr

Reputation: 325

If you're ok with Xcode coming to the foreground, I would use this instead:

tell application "Xcode"
    activate
    open "/Users/<username>/Documents/Code/Selenium/iphone/iWebDriver.xcodeproj"
end tell

tell application "System Events"
    key code 15 using {command down}
end tell

Upvotes: 3

Guillaume
Guillaume

Reputation: 21736

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.

Upvotes: 12

Related Questions