Skip to content

Instantly share code, notes, and snippets.

@cspickert
Last active December 10, 2015 09:49
Show Gist options
  • Save cspickert/4416747 to your computer and use it in GitHub Desktop.
Save cspickert/4416747 to your computer and use it in GitHub Desktop.
A shell function for quickly opening Xcode project files.
#!/bin/sh
# Usage:
# xcopen [projfile|dirname]
#
# The argument is optional. If an argument is specified and its extension
# is 'xcodeproj', it is treated as a project file. Otherwise, it is treated
# as the directory name. If no argument is specified, the directory name is
# set to '.' (the current working directory). If a project file is specified,
# it is opened. Otherwise, the first project file in the specified directory
# is opened.
#
# Note: the Xcode.app used to open the project file is determined via a call
# to xcode-select(1).
#
# Examples:
# xcopen ~/Documents/Project/Project.xcodeproj
# xcopen ~/Documents/Project
# xcopen
function xcopen_one {
local projfile=""
local dirname=""
if [[ $# -eq 0 ]]; then
dirname=".";
else
projfile="$(echo $1 | egrep -oh '.*.xc(odeproj|workspace)')"
if [[ -z "${projfile}" ]]; then
dirname="$1"
fi
fi
if [[ -z "${projfile}" ]]; then
find "${dirname}" \
-depth 1 \
\( -name '*.xcodeproj' -or -name '*.xcworkspace' \) \
-exec open -a "${xcode}" {} \; \
-print | \
sed 's,//*,/,g'
else
open -a "${xcode}" "${projfile}"
fi
}
function xcopen {
local xcode="$(xcode-select --print-path | grep -oh '.*Xcode.*\.app')"
if [[ -z "$1" ]]
then
xcopen_one
fi
while [[ ! -z "$1" ]]
do
xcopen_one "$1"
shift
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment