Skip to content

Instantly share code, notes, and snippets.

@marijnbent
Created November 1, 2023 14:51
Show Gist options
  • Select an option

  • Save marijnbent/5452c4597d93d3b47577efdb15b4d775 to your computer and use it in GitHub Desktop.

Select an option

Save marijnbent/5452c4597d93d3b47577efdb15b4d775 to your computer and use it in GitHub Desktop.
Replace applications on macOS with brew installed apps
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}")
@amwink

amwink commented May 9, 2025 •

Copy link
Copy Markdown

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...

@marijnbent

Copy link
Copy Markdown
Author

Thanks @amwink. Thanks for the improvement!

If you like this, you'll like this one as well: https://github.com/marijnbent/homebrew-interactive-install

@amwink

amwink commented Oct 7, 2026 •

Copy link
Copy Markdown

I really like the noninteractive script version! Just saw that my --no-quarantine suggestion has not been valid for a while as brew have removed it. The Apple philosophy (even brew!) does not always promote free software... That discussion was pretty harsh in the end.

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