How to run xcode from terminal?

XcodeMacos

Xcode Problem Overview


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

Xcode Solutions


Solution 1 - Xcode

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)

Solution 2 - Xcode

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.

Solution 3 - Xcode

Following command should do it:

open a.xcodeproj

Solution 4 - Xcode

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

Solution 5 - Xcode

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

user$ open -a xcode ProjectName.xcworkspace/

Solution 6 - Xcode

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

Solution 7 - Xcode

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

Solution 8 - Xcode

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfeelfreeView Question on Stackoverflow
Solution 1 - XcodeSheetJSView Answer on Stackoverflow
Solution 2 - XcodeGobeView Answer on Stackoverflow
Solution 3 - XcodeEugeneView Answer on Stackoverflow
Solution 4 - XcodedanielbeardView Answer on Stackoverflow
Solution 5 - Xcodearango_86View Answer on Stackoverflow
Solution 6 - XcodeDheeraj GuptaView Answer on Stackoverflow
Solution 7 - XcodeNikView Answer on Stackoverflow
Solution 8 - XcodekevherroView Answer on Stackoverflow