Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Xcode and bash

Hi everyone!


Im pretty new to the OS X platform, spent a whole lot of time at the Linux command line before i migrated to OS X,

and i do most of my work with regular bash scripts.


My question is about building interfaces to some of these scripts. Display output, get user input and such.

Is this something that can be done using Xcode or Interface builder?


I have done searches but i havent really found anything that applies to what im trying to do.

Maybe i simply should take it as a sign that things are not done this way?


Appreciate if anyone can shed some light on this or point me in the right direction here.

macbook pro, Mac OS X (10.6.6), Backtrack, FreeBSD, OpenBSD, NetBSD

Posted on May 15, 2011 6:43 PM

Reply
3 replies

May 15, 2011 8:02 PM in response to black-karma

You should be able to do this to some extent. How effectively will probably depend on how your scripts are written and possibly on what they're doing.


For example, if the GUI will basically collect input parameters for the script, then call the script with those parameters and then display the results then it should be fairly easy and straightforward to build the GUI for doing that.


If the shell scripts require user interaction while they're running then it's more difficult. Of course if you're willing to rework the shell scripts too then you may be able to ease some of that pain.


Also keep in mind that Xcode is just an IDE so you'll need to write code of some sort to drive the UI and call your shell scripts. I've done what you're asking about on several occasions using AppleScript Studio... which has now morphed into AppleScript Obj-C. I haven't had the opportunity to use AS Obj-C as much as I had used AS Studio but I'm sure it's still just as capable (or even more capable) of doing what you want.


AppleScript includes a "do shell script" command. You basically construct a command string containing the path to your shell script followed by your input parameters and then call "do shell script" passing it your command string. The results of your shell script will be returned as the result of the "do shell script" command.


You could also write Cocoa code to drive the UI.


Steve

Aug 16, 2013 6:30 PM in response to black-karma

You can definitely display output. Works for any interpreted scripting language. Follow these instructions:



Launch XCode and start a new project:

  1. Enter CMD + Shift + N or File > New > Project
  2. Select Other on the left column then select External Build System. Hit Next.
  3. Name your project
  4. Enter your orginzation as the organization name
  5. Enter the identifier: com.YOURORGANIZATION. followed by the project name as the project
  6. Find the path to the build tool
    1. Launch Terminal
    2. which name_of_scripting_language:
      1. e.g. which python
      2. e.g. which bash
    3. Copy the output (it will give you the path to your language executable"
    4. Select XCode
    5. Paste the output into Build Tool
  7. Select Next
  8. Select a directory to place your project. Most likely you want to place this folder in a directory where other related projects will reside.
  9. Under Source Control select: Create a local git repository for this project

Edit the Scheme

  1. Product > Scheme > Edit Scheme
    1. Make sure the appropriate scheme is selected before selecting edit scheme. Since this is likely the first scheme created, there will be only one listed, which is the title of the project.
  2. Configure settings under the Info tab:
    1. Select Executable > Other
      1. CMD + Shift + G
      2. Paste the path to your script language executable
      3. Select Choose
    2. Select None for Debugger
  3. Configure settings under the Arguments tab:
    1. Select + under Arguments Passed On Launch
      1. The Arguments Passed On Launch entry does exactly as it explains. In good old fashioned unix, the name of the command is always the first argument, indexed at 0. The following arguments are parameters passed to the command. These are usually flags that change the manner in which the program or script is executed (e.g ls -a). Typically, you’d only wanted to have the name of the script here. However, you can create different schemas with more arguments than just the script name to test other variations in which the script is called.
    2. Enter the name of the script with the script, including the extension e.g. test.py or test.sh
    3. Set Expand Variables Based On to your target. In many cases, it’s just the name of your project.
  4. Configure settings under the Options Tab
    1. Toggle on working directory
    2. Click the icon in the text field and select the path to your project directory.
  5. Select Okay

Now that we have our project and schema created, we can create our first script file.

  1. CMD + N or File > New > File
  2. Select Other from the left column and select Empty
  3. Save the file as the name of the script, including the script extension
  4. Select Create

Let’s get scripting

  1. Under your Project name in the left column, you should see the name of your script file. Select it if it is not already selected.
  2. Type the shebang line for your script at the top. Typically, this is #! followed by the path to your script language executable: eg #!/bin/bash or #!/usr/bin/python
  3. Type your first line of code to print out a simple message. This will test to make sure that the environment is configured correctly
    1. Bash: echo ‘Does this work?’
    2. Python: print(‘Does this work?’)
  4. Run the script:
    1. CMD + R
    2. Alternatively click Project > Run
  5. you should see the output to your script at the bottom of the XCode window


This sounds like a lot of work (and it sort of is), but it becomes very natural with a few created projects. The benefits of using XCode for scripting are numerous: suggested text completion, viewing output in the same window, integrated source source control with git (built in), debugging scripts with numerous schemas, etc. I know this came a little late, but hopefully this helps someone!


Message was edited by: finisherr

Xcode and bash

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.