71

My question is very simple: suppose there is an xcode project a.xcodeproj, could I open it with the command: xcode a.xcodeproj?

If I try this, I receive the following error message:

-bash: xcode: command not found
4
  • xcode is not a CLI application... xcodebuild is, not sure if that is what you want as you do not specify what exactly you want to acomplish. Commented Oct 28, 2013 at 16:38
  • @Brad Allred Sorry for the unclear question. I just want to open the .xodeproj project so that I can compile the codes.
    – feelfree
    Commented Oct 28, 2013 at 16:57
  • I recently released a custom script I wrote to open Xcode from the command line I named oxc. Future readers may find it helpful. Commented May 8, 2016 at 22:50
  • Nik, THANK`S. You saved my 3 hours of searching Commented Jul 1, 2021 at 9:03

8 Answers 8

145

Xcode should be the default application for .xcodeproj files, so this should work:

$ open a.xcodeproj

If that opens a different application, you can force it to use xcode:

$ open -a Xcode a.xcodeproj

If you want the command xcode to work, you can just alias it:

$ alias xcode="open -a Xcode"

then you can just xcode a.xcodeproj (and add this to ~/.bash_profile)

5
  • 2
    Wonderful solution. Thanx @Nirk Commented Jun 12, 2014 at 8:04
  • It is working. Now what command could be used to run an iPhone/iPad application from Xcode ? Commented May 19, 2015 at 5:06
  • One would need a different alias for opening xcworkspaces then, and manually tell which one to use (the xcodeproj or the xcworkspace one). This answer can handle both with a single command.
    – Gobe
    Commented Oct 10, 2018 at 11:52
  • 1
    open does not respect changing the toolchain with xcode-select, so if you use multiple versions of Xcode you can run into problems. xed a.xcworkspace works well.
    – teki
    Commented Nov 13, 2019 at 2:37
  • If you make changes to your ~/.bash_profile, it's a good idea to reload your bash profile from the command line so that the changes you make actually take effect todo this : source ~/.bash_profile
    – GhostCode
    Commented Dec 6, 2019 at 7:43
44

You could also simply run xed . in the project's root directory, apparently it will try to load a project in a hierarchical manner, i.e. the first that exists:

  1. the folder, if it's a Package (Xcode 11+)
  2. xcworkspace
  3. xcodeproj
  4. playground

which means you don't need to verify yourself the existing file structure in order to choose the best one to open.

1
  • 4
    This should be the new top answer Commented May 15, 2020 at 13:40
5

Can't remember where I came across this script, but I use this ruby script for finding either a *.xcodeproj or *.xcworkspace file in the working directory and opening that file (without Xcode opening any previous projects)

#!/usr/bin/env ruby

# Open xcode without any previous projects being opened as well.
# We first look for a workspace, then a project in the current directory, opening the first that is found.

f = []
f.concat Dir["*.xcworkspace"]
f.concat Dir["*.xcodeproj"]

if f.length > 0
  puts "opening #{f.first}"
  `open -a /Applications/Xcode.app #{f.first} --args -ApplePersistenceIgnoreState YES`
  exit 0
end

puts "No Xcode projects found"
exit 1
5
  • 1
    Lost this script when I upgraded my system and have been looking for it for days!! Thanks
    – Eric
    Commented Jul 13, 2016 at 3:29
  • 4
    Do you know where I could find a list of available --args to pass to Xcode? Like -ApplePersistenceIgnoreState YES?
    – Clay Ellis
    Commented Jun 3, 2017 at 17:29
  • @ClayEllis especially practical from my point of view would be to have some "verbose" option, but so far I haven't found any
    – Antek
    Commented Feb 27, 2018 at 13:38
  • 1
    So basically implemented a barebones version of 'xed' (see other answers)
    – emllnd
    Commented Dec 18, 2020 at 7:52
  • @emlind wow, how did I not know about this command before? Fantastic, thank you. Commented Jan 12, 2021 at 19:20
5

open terminal, then go to the path where Xcode is installed. Then, go to its "Contents/MacOS". And when you reach this folder, then type - sudo ./Xcode

Or else follow the following code: (you can use "sudo" if the user has privilege issue)

cd /  
cd Applications
cd Xcode.app
cd Contents/MacOS
sudo ./Xcode
4

Following command should do it:

open a.xcodeproj
3

I just type open *xcw*. This command looks up a workspace in the current directory and then opens is with Xcode.

2

incase, if you want to open a Xcode project from a workspace use the following command line.

user$ open -a xcode ProjectName.xcworkspace/
0

I have a few functions in my .zshrc that accomplish what you're looking for:

cap () { tee /tmp/capture.out; }

ret () { cat /tmp/capture.out; }

x () {
    # Substitute .xcworkspace with .xcodeproj for your case.
    find . -type d -name "*.xcworkspace" -d 1 | cap
    xed "$(ret)"
}

Then, from the same directory as your *.xcodeproj, simply execute x, e.g.:

$ x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.