Skip to content

Instantly share code, notes, and snippets.

@drewdiver
Last active December 18, 2020 09:14
Show Gist options
  • Save drewdiver/fecdb294661f5bf381aff63afbf8e3c4 to your computer and use it in GitHub Desktop.
Save drewdiver/fecdb294661f5bf381aff63afbf8e3c4 to your computer and use it in GitHub Desktop.
Install mas-cli and automate App Store installations.
#!/bin/bash
CURRENT_USER=$(stat -f %Su /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")
MAS_PKG=/private/tmp/mas.pkg
APP_LIST=(
441258766
955297617
409183694
409201541
409203825
1493996622
1376402589
191449274
1503136033
1121192229
1352778147
747648890
1289583905
497799835
)
###############################################################################
# SANITY CHECK
###############################################################################
# Need admin privileges to install mas-cli
if [[ "$EUID" -ne 0 ]]
then echo "Please run as root"
exit 1
fi
# Retrieve and clean-up url to latest installer pkg from GitHub
MAS_PKG_URL=$(curl -s https://api.github.com/repos/mas-cli/mas/releases/latest \
| grep "mas.pkg" \
| cut -d : -f 2,3 \
| tr -d \", \
| sed -n 2p \
| awk '{$1=$1};1')
###############################################################################
# FUNCTIONS
###############################################################################
# Since we are running as root, checking App Store sign in will fail
run_as_user () {
if [[ "$CURRENT_USER" != "loginwindow" ]]
then
launchctl asuser "$CURRENT_USER_UID" sudo -u "$CURRENT_USER" "$@"
else
echo "No user logged in, how is this running?"
fi
}
install_mas () {
curl -L "$MAS_PKG_URL" -o "$MAS_PKG"
if [[ -f "$MAS_PKG" ]]
then
echo "Installing mas-cli..."
installer -pkg "$MAS_PKG" -target "/"
sleep 2
echo "Cleaning up /tmp..."
rm "$MAS_PKG"
else
echo "Failed to download mas.pkg..."
exit 1
fi
}
# Are we signed into the App Store?
signed_in () {
run_as_user mas account &>/dev/null && return 0 || return 1
}
# Install all app ID's provided above
install_apps() {
for APP in "${APP_LIST[@]}"
do
run_as_user mas install "$APP"
done
}
###############################################################################
# MAIN
###############################################################################
# Ensure mas installed either by Mac Ports or Homebrew/pkg installer
if [[ ! -x /usr/local/bin/mas ]]
then
echo "couldn't find mas-cli binary, attempting to download and install..."
install_mas
fi
if signed_in
then
echo "Installing specified apps..."
install_apps
else
echo "Not signed into App Store!"
echo "Opening App Store.app, please sign in..."
open -a App\ Store.app
# Wait for sign in...
until signed_in
do
sleep 5
done
echo "Sign in successful, installing specified apps..."
install_apps
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment