Skip to content

Instantly share code, notes, and snippets.

@cbowns
Last active December 26, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbowns/7091798 to your computer and use it in GitHub Desktop.
Save cbowns/7091798 to your computer and use it in GitHub Desktop.
A shell function for opening an Xcode workspace or project in the current directory or in a directory passed as a parameter.To open Project.xcodeproj or Project.xcworkspace:`cd Project; xcode`. Alternately, `xcode Project/`.
# Base function:
# Takes two arguments: a directory and a name for Xcode.
function open_with_xcode {
targetDirectory=$1
xcodeApp=$2
# If there's a workspace file, open that.
didFindXCWorkspace=`find $targetDirectory -depth 1 | grep -i .xcworkspace | wc -l | sed -e 's/^ *//g' -e 's/ *$//g'`
# Testing a glob with -d, like
# if [ -d *.xcworkspace ];
# just yields errors when it doesn't.
# Stupid shell.
if [ $didFindXCWorkspace != 0 ]; then
open -a $xcodeApp $targetDirectory/*.xcworkspace([1])
return
fi
didFindXcodeproj=`find $targetDirectory -depth 1 | grep -i .xcodeproj | wc -l | sed -e 's/^ *//g' -e 's/ *$//g'`
if [ $didFindXcodeproj != 0 ]; then
open -a $xcodeApp $targetDirectory/*.xcodeproj([1])
return
fi
echo "No .xcworkspace or .xcodeproj found."
return 1
}
# Xcode shortcuts:
# Open things with Xcode.
function xcode {
xcodeAppName="Xcode"
targetDirectory=$1
if [ ! -n "$1" ]; then
# No argument, set it to cwd.
targetDirectory="."
fi
open_with_xcode $targetDirectory $xcodeAppName
}
function xcode_5 {
xcodeAppName="Xcode 5"
targetDirectory=$1
if [ ! -n "$1" ]; then
# No argument, set it to cwd.
targetDirectory="."
fi
open_with_xcode $targetDirectory $xcodeAppName
}
# For Xcode x.y betas:
function xcode_beta {
xcodeAppName=`ls /Applications/ | egrep "Xcode.-Beta." | sed 's-.app/--' | tail -1`
directory=$1
if [ ! -n "$1" ]; then
# No argument, set it to cwd.
directory="."
fi
open_with_xcode $directory $xcodeAppName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment