'Running script only for an 'Archive' build in Xcode 4

I have a script that I run using osascript from a Run Script Build Phase in Xcode 4. This script does some checks to try and catch any human-error in plists and suchlike.

However, I only really want to run this script when a developer chooses 'Archive' as opposed to running it every time they build.

Is there any way of testing for this situation? Either in Shell Script or Apple Script?



Solution 1:[1]

  1. Add a "New Run Script Phase" to your project's "Build Phases"
  2. Move (drag&drop) your script in the right position
  3. Check the "Run script only when installing"

If you don't do 3. the script will run all the time, but if you check that box, it will only run when archiving.

Solution 2:[2]

A quick test on my own system shows no difference in the detailed script output between straight "Build" and "Build and Archive".

Also, you can't test for the presence (or absence) of the archive, since it only gets created at the very end of the process, when all scripts have been run.

As far as I can see, there is no current option within Xcode 3 to do this. Maybe file an enhancement request with Apple?

While you wait on Apple, the only solution I can offer is to use xcodebuild and xcrun as part of a command-line shell script, where you would know if you are archiving or not. This is not a stay-in-Xcode solution, but it does have a lot of flexibility.

Xcode "Build and Archive" from command line

Solution 3:[3]

So in latest Xcode (13.3) you can edit your schemes and copy & paste your executable script in the run script textfield. There are pre- and post-actions possible. enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike
Solution 2 Community
Solution 3 Felix