Skip to content

Instantly share code, notes, and snippets.

@grahampugh
Last active July 8, 2024 00:03
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).

@gabriel383
Copy link

I am having problems to uninstall programas that have spaces in the program name.
example: Google Chrome o Sublime Text

@anegrila
Copy link

anegrila commented Oct 5, 2020

Worked well! Thank you very much!

@ashearera
Copy link

Is there a reason why, in line 15, you set appName="$4"? I'm testing the script locally before I add to Jamf and I kept getting "No application specified!" because I was obviously passing in the app name as the FIRST parameter rather than 4. Is this to account for some other parameters Jamf passes in, or just a typo? In line 19 you set it to $1

(when i entered 3 dummy parameters and then the app name as the 4th parameter it all worked).

@ashearera
Copy link

For anyone else wondering about this I answered my own question haha: https://www.jamf.com/jamf-nation/articles/146/script-parameters

Yep, the first 3 parameters are used by Jamf and your first available custom parameter is $4. Thanks for this Graham!

Is there a reason why, in line 15, you set appName="$4"? I'm testing the script locally before I add to Jamf and I kept getting "No application specified!" because I was obviously passing in the app name as the FIRST parameter rather than 4. Is this to account for some other parameters Jamf passes in, or just a typo? In line 19 you set it to $1

(when i entered 3 dummy parameters and then the app name as the 4th parameter it all worked).

@FlorinLupas
Copy link

I am here for tacos...

@valcparra
Copy link

if I needed to uninstall a set of apps - Grammarly.app, Grammarly for Safari.app, Grammarly Desktop.app, Grammarly Chrome Extension, would I replace the $4 in line 15 with each .app? how could I add all of these to this single script? thank you in advance.

@grahampugh
Copy link
Author

If I have to customise this script to add more than one folder to delete, for example, then I normally clone it and replace the Script parameter with hard-coded values. I have several examples of this in my org's repo, for example https://github.com/eth-its/autopkg-mac-recipes-yaml/blob/main/Scripts_for_Uninstallers/MicrosoftOffice-uninstall.sh

@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