28

Is there a way of grabbing the current scheme from a run script phase?

I've tried $(SCHEME_NAME) but it doesn't exist.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
James Campbell
  • 3,511
  • 4
  • 33
  • 50

4 Answers4

26

I couldn't find an environment variable to use, so I had to develop a work around: write a scheme name to disk in the Build Pre-action and then read it back out in the Run Script phase.

For each scheme you're interested in go to Edit Scheme and add a script with the following code:

rm -rf ${INTERMEDIATES_OUTPUT_DIR}
mkdir -p ${INTERMEDIATES_OUTPUT_DIR}
echo MY_SCHEME_NAME > ${SCHEME_FILE}

Build Pre-action

Next, go to your build target's "Build Settings" and add two "User-Defined Settings":

INTERMEDIATES_OUTPUT_DIR=${PROJECT_DIR}/build/intermediates/${CONFIGURATION}/
SCHEME_FILE=${INTERMEDIATES_OUTPUT_DIR}current_scheme.txt

Open up your "Run script" and add this:

SCHEME_NAME=`cat ${SCHEME_FILE}`

Be sure to add the intermediates build directory to your .gitignore file.

Obviously you could simplify this a bit by hardcoding a file name but this is a bit more robust (and we have other stuff that ends up in the intermediates directory, too).

Lukasz
  • 19,816
  • 17
  • 83
  • 139
David
  • 2,429
  • 24
  • 15
  • EXCELLENT!!! I'll have to point out that for the bash hackers like myself, my SCHEME_FILE path had spaces in it, so the line above should be SCHEME_NAME=`cat "${SCHEME_NAME}"` ... and those are backticks, not apostrophes! This also aplies to the top, "${INTERMEDIATES_OUTPUT_DIR}" and "${SCHEME_FILE}" – horseshoe7 Mar 16 '20 at 19:43
  • @horseshoe7 I am getting this issue.. can you guys help me out Showing All Issues Error: Scheme 'SCHEME_NAME' does not exist in 'myproject.xcworkspace'. – Anurag Dixit May 02 '20 at 11:59
  • uh, sorry, my comment should follow from the answer, but make sure you wrap ${SCHEME_FILE} in quotes so it will automatically escape the spaces in the pathname. – horseshoe7 May 03 '20 at 12:47
2

Simplest one is set 'User Defined' variable as per scheme and use it.

Amit
  • 2,389
  • 22
  • 29
0

You can write the scheme name to the info.plist file and read it back in Run Script Phase, in Edit Scheme menu, choose Build -> Pre-actions and add the following script:

/usr/libexec/PlistBuddy -c "Set :SchemeName \"$SCHEME_NAME\"" "$INFOPLIST_FILE"

and then add the key SchemeName of type string in the info.plist, and its initial value is empty.

Finally, in the run script phase add the following:

SCHEME_NAME=$(/usr/libexec/PlistBuddy -c "Print SchemeName" "$INFOPLIST_FILE")

JAHelia
  • 6,934
  • 17
  • 74
  • 134
0

INTERMEDIATES_OUTPUT_DIR doesn't seem to work for building swift previews which I wanted customization on as well. I ended up doing something similar using defaults write which I found works in all cases and doesn't involve the creation of a file.

Prebuild:

defaults write com.apple.dt.Xcode LastBuildScheme "MySchemeName"`

Build Script:

[[ $(defaults read com.apple.dt.Xcode LastBuildScheme) = "MySchemeName" ]] && echo "Scheme" || echo "Not Scheme"
BadPirate
  • 25,802
  • 10
  • 92
  • 123