Created
January 4, 2024 12:43
-
-
Save charliwest/14e000ee7ca437291e1fed0ca2cb0d96 to your computer and use it in GitHub Desktop.
Tries its best to find things related to an app and remove it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
function remove() | |
{ | |
paths=("$@") | |
for path in "${paths[@]}" | |
do | |
if [[ -e $path ]]; then | |
echo "Removing $path" | |
rm -r "$path" | |
fi | |
done | |
} | |
app="DEPNotify" # Options should read 'App Name' | |
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' ) | |
loggedInUserHomeDir="/Users/$loggedInUser" | |
# Check in /Applications | |
app_path="/Applications/${app}.app" | |
if [[ ! -d $app_path ]]; then | |
# Check in /Users/<user>/Applications | |
app_path="$loggedInUserHomeDir/Applications/${app}.app" | |
fi | |
# Check in /Applications/Utilities | |
if [[ ! -d $app_path ]]; then | |
app_path="/Applications/Utilities/${app}.app" | |
fi | |
if [[ ! -d $app_path ]]; then | |
echo "Application path must be in /Applications, /Applications/Utilities, or $loggedInUserHomeDir/Applications" | |
exit 1 | |
fi | |
if (( EUID != 0 )); then | |
if [[ ! -w $app_path ]]; then | |
echo "$app_path cannot be deleted. Try running this again with 'sudo'" | |
exit 1 | |
fi | |
fi | |
plist_path="${app_path}/Contents/Info.plist" | |
if [[ ! -f $plist_path ]]; then | |
echo "No plist at $plist_path" | |
exit 1 | |
fi | |
identifier=$(defaults read "$plist_path" CFBundleIdentifier) | |
if [[ -z "$identifier" ]]; then | |
echo "Couldn't determine bundle identifier '$identifier'" | |
exit 1 | |
fi | |
appname=$(basename "${app_path%.*}") | |
pkill -f "$app_path" || true | |
lines=$(pgrep -f "$(echo "$app_path" | sed -E 's/(.)/[\1]/')" | wc -l | xargs || true) | |
if [[ $lines -gt 0 ]]; then | |
echo "Please quit $appname and try again" | |
exit 1 | |
fi | |
remove "$app_path" | |
remove "$loggedInUserHomeDir/Library/Application Support/$appname" | |
remove "$loggedInUserHomeDir/Library/Application Support/$identifier" | |
remove "$loggedInUserHomeDir/Library/Containers/$identifier"* | |
remove "$loggedInUserHomeDir/Library/Caches/$appname" | |
remove "$loggedInUserHomeDir/Library/Caches/$identifier" | |
remove "$loggedInUserHomeDir/Library/$appname" | |
remove "$loggedInUserHomeDir/Library/Preferences/"*"$identifier"*".plist" | |
remove "$loggedInUserHomeDir/Library/Saved Application State/$identifier.savedState" | |
remove "$loggedInUserHomeDir/Library/SyncedPreferences/$identifier"*".plist" | |
remove "$loggedInUserHomeDir/Library/WebKit/$identifier" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment