Article Image
read

tl;dr; Swap/check current Xcode version in use with the script at the end of the article.

Every year there's a period of time where you will have two or more release versions of Xcode installed (and possbile some pre-release versions).

xcode-select is your friend in these situations, but syntax yada yada… made me make x.

It's a little bash script you can drop in your path and start using straight away for handy things like checking the currently active Xcode path:

$ x
Current Xcode path: 
/Applications/Xcode.app/Contents/Developer

Or quickly changing to an older version:

$ x 8.3
Password: ••••••••••
Swapped to Xcode 8.3 at:
/Applications/Xcode 8.3.app/Contents/Developer

Of course, you'll always want to go back to the new shiny as well:

$ x -d
Password:
Reset to default Xcode at: 
/Applications/Xcode.app/Contents/Developer

The script has only one requirement, when you have multiple versions of Xcode installed you will need to rename the older (or pre-release) versions to a suitable name.

So if the current Xcode is version 9.0, it will still be called "Xcode" in your /Applications directory, but if you have the previous version 8.3 installed rename it "Xcode 8.3" and the same for a pre-release of say version 9.1 you could rename it "Xcode 9.1b1"(for pre-release versions I usually name them in a way that clearly marks them as βeta software).

So swapping to a pre-release version simple becomes:

$ x 9.1b1
Password: ••••••••••
Swapped to Xcode 9.1b1 at:
/Applications/Xcode 9.1b1.app/Contents/Developer

x

Just copy the script into a text file called "x" (no sufix required) somewhere in your bash path and do a quick chmod +x x and you're ready to role.

2 Apr. 2019 Updated and made a bit more friendly with some built-in docs x -h or x --help

#!/bin/sh
# Set Xcode paths etc, to the Current Xcode or a previous version
version="1.1.0"

# Bold and Normal markers
bold=$(tput bold)
normal=$(tput sgr0)

# Colors for output
orange="\033[38;5;208m" #orange
grey="\033[38;5;7m"     #grey
green="\033[38;5;10m"   #apple-green
# Colour end
e="\033[0;00m"

function printHelp {
  echo
  echo $bold"Usage:"$normal" x [-p --current] [-d] [NN.N]"
  echo
  echo $bold"Options"$normal
  echo $green"\t-p"$e
  echo $green"\t--current\tPrints the current Xcode path"$e
  echo
  echo $green"\t-d"$e
  echo $green"\t--default\tSet Xcode path to the current Xcode (i.e. /Applications/Xcode.app )"$e
  echo
  echo $green"\tNN.N"$e
  echo $green"\tWhere NN.N is a value like 9.4 or 10.0 it will look for a version of Xcode with a matching name"$e
  echo $green"\tand if it exists set the Xcode path to it."$e
  echo $bold"Examples:"$normal
  echo "\t$: x\n\tCurrent Xcode path:\n\t/Applications/Xcode.app/Contents/Developer"
  echo
  echo "\t$: x -d\n\tReset to default Xcode at:\n\t/Applications/Xcode.app/Contents/Developer"
  echo
  echo "\t$: x 9.4\n\tSwapped to Xcode 9.4 at:\n\t/Applications/Xcode 9.4.app/Contents/Developer"
  echo
  echo
}

# The actual useful stuff
if [ -z $1 ] || [ $1 == "-p" ] || [ $1 == "--current" ]
  then
  echo "Current Xcode path: "
  xcode-select -p
elif [ -n $1 ]
  then
    if [ $1 = "-d" ] || [ $1 = "--default" ] || [ $1 = "-r" ] || [ $1 = "--reset" ]
    then
      sudo xcode-select -r
      echo "Reset to default Xcode at: "
      xcode-select -p
    elif [ $1 == "-p" ] || [ $1 == "--current" ]
      then
      xcode-select -p
    elif [ $1 == "-v" ]
      then
      echo
      echo $bold"x Version: "$normal$green$version$e
      echo
    elif [ -z $2 ] && [ -d /Applications/Xcode\ $1.app ]
      then
      sudo xcode-select -s /Applications/Xcode\ $1.app/Contents/Developer/
      echo "Swapped to Xcode $1 at:"
      xcode-select -p
    elif [ $1 = "-h" ] || [ $1 = "--help" ]
    then
      echo
      echo $bold"x ($version)"$normal
      echo
      printHelp
    else
      echo $orange"Unsupported option(s) supplied: $@"$e
      echo
      printHelp
    fi
fi

——

Header Photo by Torsten Dettlaff

Blog Logo

CPPL

The sum of it.


Published

Image

The Sum Of It

A place of for me to write things others may be interested in or find useful.

Back to Overview