Phil
Phil

Reputation: 627

How to run command-line application from the terminal?

I'm a recent convert from Windows to Macbook Pro. I am getting use to Xcode. One thing that I would enjoy is when running a debug application in the terminal. Currently, I have to press Command+R for it to compile and run in Xcode. To have the application run in the terminal, I have to do an additional step by opening the Products folder, right click the application, then 'Open as Exterior Editor'. Then the terminal opens and runs the program.

I would like this behavior to work automatically by pressing Command+R. It seems to me like there would be a setting to direct the output.

Are there any steps to accomplish this?

Upvotes: 9

Views: 23427

Answers (3)

MikeR
MikeR

Reputation: 639

In Xcode (at least at 8 version) there is a checkbox "Use Terminal" available from Edit Scheme... > Run > Options > Console. With that option Xcode starts system Terminal.app with your binary attached (for debugging purposes for example).

enter image description here

Upvotes: 5

chown
chown

Reputation: 52738

First, make a new scheme (or edit the current one) and change the executable to Terminal.app:

Scheme Info

Then, under the "Arguments" tab, make sure "Base Expansions On" is set to your App. Then put open -a ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}

Scheme Arguments   

The command will get expanded to something like open -a /Users/Me/Library/Developer/Xcode/DerivedData/MyProj-abcdefghijklmnopqrrstuvwxyz/Build/Products/Debug-iphonesimulator/Universal.app

open -a is how you open an App from the command line.

Edit: Use ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME} instead (see comments).

Upvotes: 9

user557219
user557219

Reputation:

Building on chown’s insightful idea:

  1. Create an AppleScript file containing:

    on run argv
        set product to item 1 of argv
        tell application "Terminal"
            activate
            do script product
        end tell
    end run
    

    This AppleScript opens Terminal.app and runs the first command-line argument inside Terminal.app

    In my configuration, I saved it as runproduct.scpt under $HOME/bin.

  2. Add a new scheme (you can duplicate your current scheme) or edit your current scheme. In the Info tab, set the executable to /usr/bin/osascript, which is a program that executes AppleScripts:

    enter image description here

  3. In the Arguments tab, add two arguments: the AppleScript location (${HOME}/bin/runproduct.scpt) and the target executable location ("${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}"), the latter being the first argument passed to the AppleScript:

    enter image description here

I’m not sure if that can be made to work with the debugger, though.

Upvotes: 2

Related Questions