Fully Automated Xcode Installation
First, login to the Apple Developer site and get the version of Xcode.dmg that works for your OSX version. You might want to take a couple of versions to support various OSX platforms (for example, 10.10 Yosemite, 10.9 Mavericks, etc.). Download dmg to a host file that you can access (for example: Amazon S3, Dropbox, your hosting provider of your choice, etc.)
Change DOWNLOAD_BASE_URL in the following script and rename the corresponding dmgs with the version and build number or add it to the script below:
#!/bin/bash DOWNLOAD_BASE_URL=http://example.com/path/to/xcode/dmgs/ ## Figure out OSX version (source: https://www.opscode.com/chef/install.sh) function detect_platform_version() { # Matching the tab-space with sed is error-prone platform_version=$(sw_vers | awk '/^ProductVersion:/ { print $2 }') major_version=$(echo $platform_version | cut -d. -f1,2) # x86_64 Apple hardware often runs 32-bit kernels (see OHAI-63) x86_64=$(sysctl -n hw.optional.x86_64) if [ $x86_64 -eq 1 ]; then machine="x86_64" fi } detect_platform_version # Determine which XCode version to use based on platform version case $platform_version in "10.10") XCODE_DMG='XCode-6.1.1-6A2008a.dmg' ;; "10.9") XCODE_DMG='XCode-5.0.2-5A3005.dmg' ;; *) XCODE_DMG='XCode-5.0.1-5A2053.dmg' ;; esac # Bootstrap XCode from dmg if [ ! -d "/Applications/Xcode.app" ]; then echo "INFO: XCode.app not found. Installing XCode..." if [ ! -e "$XCODE_DMG" ]; then curl -L -O "${DOWNLOAD_BASE_URL}/${XCODE_DMG}" fi hdiutil attach "$XCODE_DMG" export __CFPREFERENCES_AVOID_DAEMON=1 sudo installer -pkg '/Volumes/XCode/XCode.pkg' -target / hdiutil detach '/Volumes/XCode' fi
You may be interested in installing the Xcode command-line tools and bypassing the Xcode license agreement:
curl -Ls https://gist.github.com/trinitronx/6217746/raw/58456d6675e437cebbf771c60b6005b4491a0980/xcode-cli-tools.sh | sudo bash # We need to accept the xcodebuild license agreement before building anything works # Silly Apple... if [ -x "$(which expect)" ]; then echo "INFO: GNU expect found! By using this script, you automatically accept the XCode License agreement found here: http://www.apple.com/legal/sla/docs/xcode.pdf" expect ./bootstrap-scripts/accept-xcodebuild-license.exp else echo -e "\x1b[31;1mERROR:\x1b[0m Could not find expect utility (is '$(which expect)' executable?)" echo -e "\x1b[31;1mWarning:\x1b[0m You have not agreed to the Xcode license.\nBuilds will fail! Agree to the license by opening Xcode.app or running:\n xcodebuild -license\n\nOR for system-wide acceptance\n sudo xcodebuild -license" exit 1 fi
Alternative method
An alternative is to use this applescript that I created .
For use:
git clone https://gist.github.com/6237049.git cd 6237049/
You may also be interested in my answer here: How to download and install command-line tools for Xcode .
I would recommend another method using applescript method. An application depends on a hierarchy of user interface elements, which may not always remain the same for future versions of the App Store app on OSX. It seems fragile to me. Installing Xcode through the command line via dmg is probably the most reliable way.
source share