Skip to content

Instantly share code, notes, and snippets.

@johngraham262
Last active December 9, 2022 02:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johngraham262/6546595 to your computer and use it in GitHub Desktop.
Save johngraham262/6546595 to your computer and use it in GitHub Desktop.
A simple bash script that will open a `.xcworkspace` if it exists in the current directory, otherwise a `.xcodeproj` if it exists, otherwise nothing. It will print the name of the file that is being opened. When using Cocoapods with iOS apps, a second file is created with the `MyProject.xcworkspace` name, alongside the `MyProject.xcproject` file…
# Add the following lines of code to your `~/.bash_profile`,
# and then run `source ~/.bash_profile` to be able to execute
# this from the command line.
openx() {
fileToOpen='';
for file in `find . -maxdepth 1 -name *.xcworkspace`; do
fileToOpen=$file
done
if [ -n "$fileToOpen" ]
then
echo $fileToOpen
open $fileToOpen
else
for file in `find . -maxdepth 1 -name *.xcodeproj`; do
fileToOpen=$file
done
if [ -n "$fileToOpen" ]
then
echo $fileToOpen
open $fileToOpen
else
echo "No xcode files to open."
fi
fi
}
# Shorthand version of "openx", use "ox" instead.
ox() {
openx
}
@mickeyreiss
Copy link

alias ox=openx

@lukestringer90
Copy link

Nice idea but this doesn't support workspace or project files with spaces in the name unfortunately.

@alaeddineG
Copy link

Thank you for this script !! 👍

@mekanics
Copy link

Nice one.

you should probably change the for ... find with:

find . -maxdepth 1 -name *.xcworkspace -print0 | while IFS= read -r -d '' file; do
respectively
find . -maxdepth 1 -name *. xcodeproj -print0 | while IFS= read -r -d '' file; do

in case your project/workspace filename contains whitespaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment