Last active
May 15, 2024 13:57
-
-
Save sirlaurie/454073dfdddc9da1b7016f9b7ed31abf to your computer and use it in GitHub Desktop.
clean macOS app. require fd command tools
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
#!/usr/bin/env zsh | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
WHITE='\033[1;37m' | |
NC='\033[0m' | |
if [[ $# != 1 ]]; then | |
echo "no input path. Usage:\n $(basename $0) app_path" | |
exit | |
fi | |
app=$1 | |
test -d $app || { echo "$app not found.";exit } | |
if [[ $app != /* ]]; then | |
echo "Please type the application absolute path, you can drag it here directly." | |
exit | |
elif [[ $app == /System/* ]]; then | |
echo "Can not remove default macOS app!" | |
exit | |
fi | |
if [[ $app == */ ]]; then | |
app=$(echo $app | sed 's/\/$//') | |
fi | |
CMD="fd" | |
type $CMD > /dev/null && : || { echo "$CMD not found, install it first. try run:\n brew install $CMD";exit } | |
function delAndPrint() { | |
if [[ -f "$1" ]]; then | |
echo "removing ${WHITE}$1${NC} ... \c" && sudo rm "$1" 2>/dev/null && echo "${GREEN}OK${NC}" || echo "${RED}failed${NC}" | |
fi | |
if [[ -d "$1" ]]; then | |
echo "removing ${WHITE}$1${NC} ... \c" && sudo rm -rf "$1" 2>/dev/null && echo "${GREEN}OK${NC}" || echo "${RED}failed${NC}" | |
fi | |
} | |
function delBeforePromt() { | |
[ -f "$1" -o -d "$1" ] && { read -q "reply?delete $1? [Y/n] " | |
case $reply in | |
[yY][eE][sS]|[yY]) | |
echo " ... \c" && sudo rm -rf "$1" 2>/dev/null && echo "${GREEN}OK${NC}" || echo "${RED}failed${NC}" | |
;; | |
[nN][oO]|[nN]) | |
echo "... ${GREEN}skipped${NC}" | |
;; | |
esac } | |
} | |
delete_if_exists() { | |
local dir="$1" | |
if [[ -d "$dir" ]]; then | |
delAndPrint "$dir" | |
fi | |
} | |
InfoFile="/Info.plist" | |
iosInfoFile="/iTunesMetadata.plist" | |
macAppInfoFile="${app}/Contents${InfoFile}" | |
iosAppInfoFile="${app}/Wrapper${iosInfoFile}" | |
if [[ -d "$app/Wrapper" ]]; then | |
bundleId=$(defaults read "${iosAppInfoFile}" softwareVersionBundleId 2> /dev/null) || { echo "Can not found Info.plist file, are you sure it's an app?";exit } | |
else | |
bundleId=$(defaults read "${macAppInfoFile}" CFBundleIdentifier 2> /dev/null) || { echo "Can not found Info.plist file, are you sure it's an app?";exit } | |
fi | |
appname=$(echo $app | awk -F '/' '{print $NF}') | |
appself=$(echo $appname | awk -F '.' '{print $1}') | |
sudo pkgutil --forget "${bundleId}" &> /dev/null || sudo pkgutil --unlink "${bundleId}" &> /dev/null | |
delete_if_exists "$HOME/Library/Caches/${bundleId}" | |
delete_if_exists "$HOME/Library/Caches/${appself}" | |
delete_if_exists "$HOME/Library/Containers/${bundleId}" | |
delete_if_exists "$HOME/Library/Containers/${appself}" | |
delete_if_exists "$HOME/Library/Group Containers/${bundleId}" | |
delete_if_exists "$HOME/Library/Group Containers/${appself}" | |
delete_if_exists "$HOME/Library/Application Support/${bundleId}" | |
delete_if_exists "$HOME/Library/Application Support/${appself}" | |
delete_if_exists "$HOME/Library/Application Scripts/${bundleId}" | |
delete_if_exists "$HOME/Library/Application Scripts/${appself}" | |
delete_if_exists "$HOME/Library/Saved Application State/${bundleId}" | |
delete_if_exists "$HOME/Library/Saved Application State/${appself}" | |
$CMD -HIi \ | |
"${bundleId}" \ | |
"$HOME/Library/WebKit/" \ | |
"$HOME/Library/Cookies/" \ | |
"$HOME/Library/Containers/" \ | |
"$HOME/Library/Preferences/" \ | |
"$HOME/Library/HTTPStorages/" \ | |
"$HOME/Library/Group Containers/" \ | |
"$HOME/Library/Application Scripts/" \ | |
"$HOME/Library/Saved Application State/" \ | |
"/Library/PrivilegedHelperTools/" \ | |
"$HOME/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/" \ | |
${TMPDIR} | while read line | |
do | |
delAndPrint "${line}" | |
done | |
$CMD -HIi \ | |
"${appself}" \ | |
"$HOME/Library/Caches/" \ | |
"$HOME/Library/WebKit/" \ | |
"$HOME/Library/Cookies/" \ | |
"$HOME/Library/Containers/" \ | |
"$HOME/Library/Preferences/" \ | |
"$HOME/Library/HTTPStorages/" \ | |
"$HOME/Library/Group Containers/" \ | |
"$HOME/Library/Application Support/" \ | |
"$HOME/Library/Application Scripts/" \ | |
"$HOME/Library/Saved Application State/" \ | |
"$HOME/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/" \ | |
${TMPDIR} | while read line | |
do | |
delBeforePromt "${line}" | |
done | |
delAndPrint "$app/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment