Created
November 1, 2023 14:51
-
-
Save marijnbent/5452c4597d93d3b47577efdb15b4d775 to your computer and use it in GitHub Desktop.
Replace applications on macOS with brew installed apps
This file contains hidden or 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
import os | |
import argparse | |
import shutil | |
import subprocess | |
def list_installed_apps(): | |
app_folder_path = '/Applications' | |
app_list = [app.replace('.app', '') for app in os.listdir(app_folder_path) if app.endswith('.app')] | |
return app_list | |
def list_cask_apps(): | |
return subprocess.check_output(['brew', 'list', '--cask']).decode() | |
def list_skipped_apps(): | |
return ['Numbers', 'Pages', 'Keynote'] | |
def delete_app(app): | |
app_path = f'/Applications/{app}.app' | |
if os.path.exists(app_path): | |
subprocess.run(['sudo', 'rm', '-rf', app_path]) | |
def reinstall_brew_cask(app): | |
subprocess.run(['brew', 'install', '--cask', '--force', app]) | |
def search_brew_cask(app): | |
try: | |
output = subprocess.check_output(['brew', 'search', app, '--cask']).decode() | |
return app in output.lower() | |
except subprocess.CalledProcessError: | |
return False | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--reinstall', action='store_true', default=False) | |
args = parser.parse_args() | |
args.reinstall = True | |
installed_apps = list_installed_apps() | |
cask_apps = list_cask_apps() | |
skip_apps = list_skipped_apps() | |
for app in installed_apps: | |
if app in skip_apps: | |
print(f'🌈 {app} skipped') | |
continue | |
if app in cask_apps: | |
print(f'✨ {app} already casked') | |
continue | |
brew_app_name = app.replace(' ', '-').lower() | |
if search_brew_cask(brew_app_name): | |
print(f"⌛ {app} has a cask alternative.") | |
if args.reinstall: | |
delete_app(app) | |
print(f'☠️ Deleted {app}') | |
reinstall_brew_cask(brew_app_name) | |
print(f'✅ Reinstalled {app}') | |
else: | |
print(f"⚠️ {app}") |
Thanks @amwink. Thanks for the improvement!
If you like this, you'll like this one as well: https://github.com/marijnbent/homebrew-interactive-install
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is brilliant. So good to have maximal usage of brew.
One thing -which may be specific to my setup: Applications that were in /Applications before, were in /opt/homebrew/bin after running the script, so could not be found. This was easily fixed by doing
for f in $(brew --cask list); do if [[ ${f//term/} == ${f} ]];then brew uninstall $f; brew install --cask --no-quarantine $f; fi; done
, which puts the applications back to /Applications, but I'm not sure why this does not happen straight away...