Skip to content

Instantly share code, notes, and snippets.

@charliwest
Last active November 28, 2023 12:09
Show Gist options
  • Save charliwest/10d174693bfe6a477e6d9ecdb01b3fc3 to your computer and use it in GitHub Desktop.
Save charliwest/10d174693bfe6a477e6d9ecdb01b3fc3 to your computer and use it in GitHub Desktop.
browser_remover_combined.sh
#!/bin/bash
# Check if the app names are provided as a command line argument
if [ -z "$4" ]; then
echo "Usage: $0 <app_names>"
exit 1
fi
# Get the app names from the fourth argument and split them into an array
IFS="," read -ra app_names <<< "$4"
# SwiftDialog variables, these must be filled in or removed as variables
SDIcon="/Library/Management/Files/MMS.icns"
SDTitle="Your Title Here"
SDMessage="Your Message Here"
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
loggedInUserHomeDir=$(echo ~$loggedInUser)
# Function to remove a file or directory if it exists, used at the end
remove_if_exists() {
local file_or_dir="$1"
if [ -e "$file_or_dir" ]; then
rm -rf "$file_or_dir"
fi
}
# Array to store results for JSON content
results=()
# Create the JSON content with proper indentation
json_content="{\"checkbox\": ["
# Iterate through app names
for app_name in "${app_names[@]}"; do
app_path="/Applications/$app_name.app"
if [[ -d "$app_path" ]]; then
# Generate the dialog content for each app
results+=("{\"label\": \"$app_name\", \"checked\": false, \"disabled\": false, \"icon\": \"$app_path\"}")
fi
done
# Add the results to the JSON content
for ((i = 0; i < ${#results[@]}; i++)); do
json_content+="${results[i]}"
if ((i < ${#results[@]} - 1)); then
json_content+=', '
fi
done
json_content+="]}"
# Pass the JSON content to the 'dialog' command
selected_apps_json=$(echo "$json_content" | /usr/local/bin/dialog --messageposition top --checkboxstyle switch,small --jsonstring "$json_content" -i "$SDIcon" -t "$SDTitle" -m "$SDMessage")
# Iterate through the selected apps and execute removal actions
for app_name in "${app_names[@]}"; do
if [[ "$selected_apps_json" == *"$app_name\" : \"true"* ]]; then
echo "Selected app for removal: $app_name"
app_path="/Applications/$app_name.app"
pkill -f "$app_path" || true
lines=$(pgrep -f "$(echo "$app_path" | sed -E 's/(.)/[\1]/')" | wc -l | xargs || true)
if [[ $lines -gt 0 ]]; then
/usr/local/bin/dialog --mini -t "$app_name is still running" -m "Please quit $app_name and try again" -i "$app_path"
exit 1
fi
/usr/local/bin/dialog --width 300 --height 120 -t "Now removing $app_name" --titlefont "size=18" -i none --timer 3
# Find and remove receipt files for the original app name
bundle_identifiers=$(pkgutil --pkgs | grep -i "$app_name")
app_no_spaces=$(echo "$app_name" | tr -d ' ') # Remove spaces from app name
# Loop through the bundle identifiers and generate the forget commands for the original app name
for bundle_id in $bundle_identifiers; do
forget_command="sudo pkgutil --forget $bundle_id"
echo "Forgetting: $forget_command"
eval "$forget_command"
done
lower_app=$(echo "$app_name" | tr '[:upper:]' '[:lower:]')
lower_app_no_spaces=$(echo "$app_no_spaces" | tr '[:upper:]' '[:lower:]') # Remove spaces from app name
# Remove files and directories only if they exist
remove_if_exists "$app_path"
remove_if_exists "$app_no_spaces.app" # Remove with spaces removed
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$app_name.app"
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$app_name"
remove_if_exists "$loggedInUserHomeDir/Library/Containers/$app_name"*
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$app_name.app"
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$app_name"
remove_if_exists "$loggedInUserHomeDir/Library/$app_name.app"
remove_if_exists "$loggedInUserHomeDir/Library/Preferences/"*"$app_name"*".plist"
remove_if_exists "$loggedInUserHomeDir/Library/Saved Application State/$app_name.savedState"
remove_if_exists "$loggedInUserHomeDir/Library/WebKit/$app_name"
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$lower_app.app"
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$lower_app"
remove_if_exists "$loggedInUserHomeDir/Library/Containers/$lower_app"*
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$lower_app.app"
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$lower_app"
remove_if_exists "$loggedInUserHomeDir/Library/$lower_app.app"
remove_if_exists "$loggedInUserHomeDir/Library/Preferences/"*"$lower_app"*".plist"
remove_if_exists "$loggedInUserHomeDir/Library/Saved Application State/$lower_app.savedState"
remove_if_exists "$loggedInUserHomeDir/Library/WebKit/$lower_app"
# If the app name contains spaces, loop through the bundle identifiers for the app with spaces removed
if [ "$app_name" != "$app_no_spaces" ]; then
bundle_identifiers_no_spaces=$(pkgutil --pkgs | grep -i "$app_no_spaces")
for bundle_id in $bundle_identifiers_no_spaces; do
forget_command="sudo pkgutil --forget $bundle_id"
echo "Forgetting: $forget_command"
eval "$forget_command"
done
# Remove files and directories for the app with spaces removed
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$lower_app_no_spaces.app"
remove_if_exists "$loggedInUserHomeDir/Library/Application Support/$lower_app_no_spaces"
remove_if_exists "$loggedInUserHomeDir/Library/Containers/$lower_app_no_spaces"*
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$lower_app_no_spaces.app"
remove_if_exists "$loggedInUserHomeDir/Library/Caches/$lower_app_no_spaces"
remove_if_exists "$loggedInUserHomeDir/Library/$lower_app_no_spaces.app"
remove_if_exists "$loggedInUserHomeDir/Library/Preferences/"*"$lower_app_no_spaces"*".plist"
remove_if_exists "$loggedInUserHomeDir/Library/Saved Application State/$lower_app_no_spaces.savedState"
remove_if_exists "$loggedInUserHomeDir/Library/WebKit/$lower_app_no_spaces"
fi
fi
done
@charliwest
Copy link
Author

Tweaked it so you can pass the apps as a jamf variable $4. Untested.

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