A better way of managing Xcode scripts

May 21, 2013 00:00 · 253 words · 2 minute read

I can’t remember where, but I recently came across a neat way of managing Xcode scripts. Usually, you’d add a script into a Run Script build phase for your target. That’s fine, but it does mean that the script is ‘buried’ inside the Xcode project. The effect of this is to hide any changes away inside the mess of XML that is a .xcproject or .xcworkspace file.

The alternative is to create the script as a separate external file in your project folder; and link to this from the Run Script build phase. The process goes like this:

  • Create your script file in the root of your project’s directory structure, and give it a .sh file type
  • Chmod it so that it’s executable with chmod +x myscript.sh
  • In Xcode’s Build Phases section, add a new Run script build phase
  • Add your new script to the Run script phase with ./myscript.sh

Then when you build the project, Xcode will execute the contents of your myscript.sh file.

You can change the name of the Run Script build phase by double-clicking it. This is an example of how I include the script that automagically bumps the version and build number based on the git tag and commit numbers:

The advantage of this is that myscript.sh will be included under source control as a discrete file - so any changes that are made (or more importantly, any merge conflicts) will be much easier to identify than they would be if they were wrapped up inside Xcode’s XML.