6

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?

Michael
  • 4,700
  • 9
  • 35
  • 42
  • possible duplicate of [Tell AppleScript To Build XCode Project](http://stackoverflow.com/questions/1007082/tell-applescript-to-build-xcode-project) – redolent Aug 06 '15 at 05:17

3 Answers3

8

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)

AnthoPak
  • 4,191
  • 3
  • 23
  • 41
Harry Jordan
  • 305
  • 2
  • 8
  • 2
    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. – Thomas Tempelmann Mar 22 '13 at 11:33
  • With XCode 5 I get "107:126: execution error: Xcode got an error: The specified object is a property, not an element. (-10008)" – markshep Oct 22 '13 at 18:57
  • 4
    In Xcode 5 replace "active project document" with "active workspace document" – Ian Nov 14 '13 at 20:11
1

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

smorgan
  • 20,228
  • 3
  • 47
  • 55
  • 1
    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. – Michael Jul 09 '09 at 01:39
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'
Harry Jordan
  • 305
  • 2
  • 8