sunknudsen / privacy-guides

Privacy guides reference material (archived)
MIT License
780 stars 91 forks source link

Check for casks when removing an app using app-cleaner.sh #20

Open albertcat opened 3 years ago

albertcat commented 3 years ago

Guide

How to clean uninstall macOS apps using AppCleaner open source alternative

Operating system and version

macOS Catalina 10.15.7

Description

If you use the app-clenaer.sh to remove an app installed via homebrew cask, it will leave the cask references dandling in the system. I've added a check that checks for a matching "cask link", and launches the uninstall to fully clean the app from the system. Maybe somebody else can benefit from the addition. Let me know what you think.

I've added it just after moving the app to Trash.

# Find brew casks linked to the app
brew_cask=$(find -L /usr/local/Caskroom -name "$app_name.app"  2>&1 | awk -F "/" '{print $5}')

if [ "$brew_cask" != "" ]; then
  printf "%s\n" "Cask found!"
  brew info --cask $brew_cask

  printf "${red}%s${nc}" "Uninstall cask (y or n)? "
  read -r answer
  if [ "$answer" = "y" ]; then
    printf "%s\n" "Uninstalling cask..."
    sleep 1
    brew uninstall --cask $brew_cask
  fi
fi
sunknudsen commented 3 years ago

Great contribution @farwel.

Does running brew uninstall --cask $brew_cask once most app-related files have been deleted generate errors?

albertcat commented 3 years ago

Hi @sunknudsen!

No, it does not generate any errors. I can't confirm 100% but it seems it does is just remove any references that will be still in the system. Here is an example when deleting Joplin. As you can see, even after sending the app to the trash it finds the cask and shows the info of the app. Just keep in mind that if you restore the app from the trash it will not restore the cask.

$ ./app-cleaner /Applications/Joplin.app 
Checking for running processes...
Finding app data...
/Applications/Joplin.app
/Users/albert/Library/Application Support/@joplin
/Users/albert/Library/Preferences/net.cozic.joplin-desktop.plist
/Users/albert/Library/Saved Application State/net.cozic.joplin-desktop.savedState
/var/folders/md/8zx6n_ws0l91l5d_s1943ynw0000gn/C/net.cozic.joplin-desktop
Move app data to trash (y or n)? y
Moving app data to trash...
Done
Cask found!
joplin: 1.7.11
https://joplin.cozic.net/
/usr/local/Caskroom/joplin/1.6.8 (120B)
From: https://github.com/Homebrew/homebrew-cask/blob/HEAD/Casks/joplin.rb
==> Name
Joplin
==> Description
Note taking and to-do application with synchronization capabilities
==> Artifacts
Joplin.app (App)
==> Analytics
install: 650 (30 days), 2,190 (90 days), 15,422 (365 days)
Uninstall cask (y or n)? y
Uninstalling cask...
==> Uninstalling Cask joplin
==> Purging files for version 1.6.8 of Cask joplin
$
ghost commented 3 years ago

Hi! 👋 "Pending consensus" label is present, so I feel obliged to take a stand: great idea and should be implemented 😉

paskn commented 2 years ago

I think this is a good suggestion. At least on my system some apps are coming from brew as I try to avoid using App Store whenever is possible. Another suggestion for this new piece of code is to add an alert / message to remind a user that an app migh be installed from brew, and uninstalling it this way will make it impossible to restore the cask.

sunknudsen commented 2 years ago

Hey @farwel, can you please create a pull request?

Btw, sorry for taking forever to follow up on this one.

If cask is found, should script end? In other words, looking forward to seeing where snippet is added.

sunknudsen commented 2 years ago

Perhaps one should add a test to see if Homebrew is installed before looking for matching casks?

albertcat commented 2 years ago

Hi! I've made the switch to Linux so unfortunately I can't provide a PR as I will not be able to test the code. Maybe someone can take over and update the script using the code on my first comment as a reference.

My initial suggestion was adding the snippet at the end, after moving the data to Trash check/process.