Skip to content

Instantly share code, notes, and snippets.

@grahampugh
Last active September 28, 2023 13:20
Show Gist options
  • Save grahampugh/97cd9c3de8f4690a9dc242f74c3211a9 to your computer and use it in GitHub Desktop.
Save grahampugh/97cd9c3de8f4690a9dc242f74c3211a9 to your computer and use it in GitHub Desktop.
A Jamf Pro script to remove applications in the Applications folder.
#!/bin/bash
#######################################################################
#
# Remove Application Script for Jamf Pro
#
# This script can delete apps that are sandboxed and live in /Applications
#
# The first parameter is used to kill the app. It should be the app name or path
# as required by the pkill command.
#
#######################################################################
# Inputted variables
appName="$4"
function silent_app_quit() {
# silently kill the application.
appName="$1"
if [[ $(pgrep -ix "$appName") ]]; then
echo "Closing $appName"
/usr/bin/osascript -e "quit app \"$appName\""
sleep 1
# double-check
countUp=0
while [[ $countUp -le 10 ]]; do
if [[ -z $(pgrep -ix "$appName") ]]; then
echo "$appName closed."
break
else
let countUp=$countUp+1
sleep 1
fi
done
if [[ $(pgrep -x "$appName") ]]; then
echo "$appName failed to quit - killing."
/usr/bin/pkill "$appName"
fi
fi
}
if [[ -z "${appName}" ]]; then
echo "No application specified!"
exit 1
fi
# quit the app if running
silent_app_quit "$appName"
# Now remove the app
echo "Removing application: ${appName}"
# Add .app to end when providing just a name e.g. "TeamViewer"
if [[ ! $appName == *".app"* ]]; then
appName=$appName".app"
fi
# Add standard path if none provided
if [[ ! $appName == *"/"* ]]; then
appToDelete="/Applications/$appName"
else
appToDelete="$appName"
fi
# Remove the application
/bin/rm -rf "${appToDelete}"
# Try to Forget the packages if we can find a match
# Loop through the remaining parameters
for package in "${@:5}"; do
if [[ ${package} ]]; then
/usr/sbin/pkgutil --pkgs | /usr/bin/grep -i "${package}" | /usr/bin/xargs /usr/bin/sudo /usr/sbin/pkgutil --forget
fi
done

Remove Application.sh

Upload this script to Jamf, then add it to a policy and use the following parameters:

  • Parameter 4: App Name, e.g. TeamViewer or TeamViewer.app (either will work)
  • Parameter 5: package receipt ID, if you want the script to remove the package receipt, e.g. com.teamviewer.teamviewer.

Parameters 6 to 11 can be used to forget additional packages if required.

To find out the package receipt IDs, install the application on a test device from Jamf, and then run pkgutil --pkgs | grep -i teamviewer, substituting a name that is likely to be in the package receipt name.

Note, this script does not delete Jamf's own package receipts (I never use those, so never really considered using them).

@Valheron
Copy link

Valheron commented Oct 9, 2022

Hey, I am kinda new to this, what do I need to change in this script to specify the app name exactly? Sorry for the noobish question. Thanks.

@grahampugh
Copy link
Author

@valcparra this script is designed to be used once for each app. Since recent versions of Jamf, it is possible to add a script multiple times to a policy, so it is easier to use that way.

@Valheron you don't need to change the script at all. Upload it to Jamf, then add it to a policy and use the following parameters:

  • Parameter 4 - App Name, e.g. TeamViewer or TeamViewer.app (both will work)
  • Parameter 5 - package receipt ID, if you want the script to remove the package receipt, e.g. com.teamviewer.teamviewer.

To find out the package receipt ID, install the application on a test device from Jamf, and then run pkgutil --pkgs | grep -i teamviewer, substituting a name that is likely to be in the package receipt name.

@kdjuve
Copy link

kdjuve commented Nov 18, 2022

My users are restricted and unable to install apps in the Applications folder. They have found a workaround and are installing apps in their local user /Applications /Desktop /Documents and /Downloads folders. What changes would I need to make to the script in order to delete apps installed in these folders?

@grahampugh
Copy link
Author

@kdjuve you would have to add all those paths to the script. Perhaps in a for loop. But be aware that you would need to give Jamf Pro full disk access to access those user folders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment