Automate the Install of Xcode Command Line Tools

Does anyone know how to automate the install of Xcode Command Line Tools?

Bonus points for

  • doing it in a supportable Apple method, and
  • if possible, leveraging the macOS version of Bash

I've tried this method: (sorry, this forum won't allow the link)

xcode-select --install
sleep 1
osascript <<EOD
  tell application "System Events"
    tell process "Install Command Line Developer Tools"
      keystroke return
      click button "Agree" of window "License Agreement"
    end tell
  end tell
EOD

This automation fails because on first-run, the Security & Privacy wants (user) approval for:

Accessibility > sshd-keygen-wrapper: annoying (bottom)

This approval must be provided before the install but there is no way to

  1. manually approve before the install, nor
  2. automate the approval during the install

At least not as far as far as I can see. Any help is appreciated.

TIA

somethings just require human oversight.

Try this. Works happily in the latest release of Monterey and also in the current Public Betas.

The script checks if Xcode is installed currently, and if it is, exits cleanly. If Xcode is not present, then it performs a touch to deposit a file in /tmp and runs a grep, tail, and sed, to extract the current latest Xcode Command Line Tools name as a variable, then pumps that into softwareupdate to install it with verbosity.

#!/bin/zsh
echo "Checking Command Line Tools for Xcode"
# Only run if the tools are not installed yet
# To check that try to print the SDK path
xcode-select -p &> /dev/null
if [ $? -ne 0 ]; then
  echo "Command Line Tools for Xcode not found. Installing from softwareupdate…"
# This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
  touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress;
  PROD=$(softwareupdate -l | grep "\*.*Command Line" | tail -n 1 | sed 's/^[^C]* //')
  softwareupdate -i "$PROD" --verbose;
else
  echo "Command Line Tools for Xcode have been installed."
fi
Automate the Install of Xcode Command Line Tools
 
 
Q