8

I'm trying to emulate Xcode's ⌘-R keystroke in another editor (namely, Vim); I thought I would be able to do this with some shell scripting & applescript, but it doesn't seem to be working correctly:

open -a Xcode "MyProj.xcodeproj"
osascript -e 'tell app "Xcode"' -e 'build' -e 'launch' -e 'end tell'

The problem with this is it launches the app regardless of whether Xcode reports errors. Is there any way to fix this?

1

4 Answers 4

9

I use:

osascript -e 'tell application "Xcode"
    activate

    set targetProject to active workspace document
    if (build targetProject) is equal to "Build succeeded" then
        launch targetProject
    end if
end tell'

Of course, the project has to already be open in Xcode for this to work. (I'd rather not hard code the current project into my script)

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to work any more with Xcode 4.6: "Xcode got an error: Can’t get active project document." Though, of course, a buildable project is open in Xcode.
With XCode 5 I get "107:126: execution error: Xcode got an error: The specified object is a property, not an element. (-10008)"
In Xcode 5 replace "active project document" with "active workspace document"
1

Unless you really want the Xcode GUI, you could just use xcodebuild instead of launching and scripting Xcode.

1 Comment

I know about xcodebuild, but I wanted to get this to work in the GUI, mainly because of the error list that brings you to the line of the error when double-clicked.
0

And for other TextMate users out there, a cobbled together.. improved version by mashing it together with the existing 'Open project in Xcode..' command:

PROJECT=$(ruby -- "${TM_BUNDLE_SUPPORT}/bin/find_xcode_project.rb")
if [[ -f "${PROJECT}/project.pbxproj" ]]; then
   open -a Xcode "${PROJECT}"
else
   echo "Didn't find an Xcode project file."
   echo "You may want to set TM_XCODE_PROJECT."
fi


osascript -e 'tell application "Xcode"
    activate

    set targetProject to project of active project document
    if (build targetProject) is equal to "Build succeeded" then
        launch targetProject
    end if
end tell'

Comments

0

For Xcode 16.4 use this AppleScript:

tell application "Xcode"
    activate
    
    set targetProject to active workspace document
    build targetProject
    run targetProject
end tell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.