Skip to content

Instantly share code, notes, and snippets.

@marijnbent
Created November 1, 2023 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marijnbent/5452c4597d93d3b47577efdb15b4d775 to your computer and use it in GitHub Desktop.
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}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment