Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Cocoa app after building it from the command line with xcodebuild?

I'm building a Mac OS X Cocoa application from the command line using an Xcode project like this:

xcodebuild -scheme MyApp -configuration Debug

How do I run it once it's done building?

like image 862
a paid nerd Avatar asked Apr 14 '15 16:04

a paid nerd


People also ask

How do I run a command in Xcode?

go to the 'Info' tab and in a menu 'Executable' choose 'Other...' in file window go to search input field and type 'terminal' and click on its icon when you find it. Now you should see 'Terminal. app' in 'Executable' field.

What is Xcrun command?

xcrun is a tool that helps managing Xcode versions on your system. It allows you to write scripts that don't need to know where your Xcode instance or developer tools are installed. The path to the Xcode version (or developer tools) is set/read via xcode-select . You can reset it via: sudo xcode-select --reset.

How do I pass command line arguments in Xcode?

Open your scheme (⌘<) and select the Run > Arguments tab. Add the arguments you want to pass on launch one at a time. Double-click to edit any argument: The arguments are vended by CommandLine.

What are macOS command line tools?

Xcode Command Line Tools are tools for software developers that run on the command line, in the Terminal application. Since before Apple's beginnings, this assortment of Unix-based tools have been the foundation of almost all software development.


2 Answers

I wrote a script to do this:

#!/bin/bash

x=$( xcodebuild -showBuildSettings -project MyApp.xcodeproj | grep ' BUILD_DIR =' | sed -e 's/.*= *//' )

DYLD_FRAMEWORK_PATH=$x/Debug DYLD_LIBRARY_PATH=$x/Debug $x/Debug/MyApp.app/Contents/MacOS/MyApp

(I figured this out by running the application from Xcode and then ps -wwE -p on the process to see its environment variables.)

like image 132
a paid nerd Avatar answered Nov 15 '22 06:11

a paid nerd


I've had the same question and the answer from a paid nerd was helpful.

After doing some research I did find a cleaner solution, which I will provide as an answer here. It's mainly the parsing code that is cleaner and made re-usable for retrieving other build settings as well.

#!/bin/bash

WORKSPACE_PATH='/Volumes/Development/MyApp/MyApp.xcworkspace'
CONFIGURATION='Debug'
SCHEME='macOS'

getBuildSetting() { 
    echo $(xcodebuild -showBuildSettings -workspace "$WORKSPACE_PATH" -scheme "$SCHEME" -configuration "$CONFIGURATION" | grep " $1" | awk '{print $3}' )
}

BUILT_PRODUCTS_DIR=$(getBuildSetting "BUILT_PRODUCTS_DIR")
FULL_PRODUCT_NAME=$(getBuildSetting "FULL_PRODUCT_NAME")
open -a "$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME" &

Of course this approach makes use of a workspace instead of a project, though it can be easily changed. Also I've just used the open command to open the app.

like image 24
Wolfgang Schreurs Avatar answered Nov 15 '22 07:11

Wolfgang Schreurs