Created
July 23, 2024 15:32
-
-
Save spokanemac/b7a120cf49c74cf66f3ab2c52e1d0a21 to your computer and use it in GitHub Desktop.
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/zsh | |
# This script will locate application components and remove them. | |
# Function to quit the application if it is running | |
quit_application() { | |
echo "Attempting to quit '$APP_NAME'..." | |
osascript -e "tell application \"$APP_NAME\" to quit" | |
# Wait for a few seconds to allow the application to quit | |
sleep 5 | |
# Check again if the application is still running | |
if pgrep -f "$APP_NAME" > /dev/null; then | |
echo "Failed to quit '$APP_NAME'. Forcing quit..." | |
pkill -f "$APP_NAME" | |
else | |
echo "Application '$APP_NAME' has been quit successfully." | |
fi | |
} | |
# Parse arguments | |
FORCE_DELETE=false | |
while getopts "f" opt; do | |
case $opt in | |
f) FORCE_DELETE=true ;; | |
*) echo "Usage: $0 [-f] [application name]" | |
exit 1 ;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
# Get application name | |
if [ $# -eq 1 ]; then | |
APP_NAME="$1" | |
else | |
read -p "Enter the application name: " APP_NAME | |
fi | |
# Check if the application is installed | |
APP_PATH=$(mdfind -name "$APP_NAME" | grep -i ".app$" | head -n 1) | |
if [ -z "$APP_PATH" ]; then | |
echo "Application '$APP_NAME' not found." | |
exit 1 | |
fi | |
echo "Application '$APP_NAME' found at: $APP_PATH" | |
# Check if the application is running | |
if pgrep -f "$APP_NAME" > /dev/null; then | |
quit_application | |
else | |
echo "Application '$APP_NAME' is not running." | |
fi | |
# Find all components related to the application | |
APP_COMPONENTS=$(mdfind -name "$APP_NAME") | |
if [ -z "$APP_COMPONENTS" ]; then | |
echo "No components found for '$APP_NAME'." | |
exit 1 | |
fi | |
# List components and ask for confirmation if not forcing deletion | |
if [ "$FORCE_DELETE" = false ]; then | |
echo "The following components will be deleted:" | |
echo "$APP_COMPONENTS" | |
read -q "REPLY?Do you want to proceed with deletion? (y/n): " | |
echo | |
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then | |
echo "Deletion aborted." | |
exit 1 | |
fi | |
fi | |
# Unload launch agents and force quit executables | |
while IFS= read -r COMPONENT; do | |
if [[ "$COMPONENT" == *".plist" ]]; then | |
echo "Unloading launch agent $COMPONENT" | |
launchctl unload "$COMPONENT" 2>/dev/null | |
elif [ -x "$COMPONENT" ]; then | |
echo "Force quitting executable $COMPONENT" | |
pkill -f "$COMPONENT" | |
fi | |
done <<< "$APP_COMPONENTS" | |
# Delete all found components | |
echo "Deleting the following components:" | |
echo "$APP_COMPONENTS" | |
while IFS= read -r COMPONENT; do | |
echo "Deleting $COMPONENT" | |
rm -rf "$COMPONENT" | |
done <<< "$APP_COMPONENTS" | |
echo "All components of '$APP_NAME' have been deleted." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment