Last active
May 5, 2023 10:43
-
-
Save rsperl/bb5aa698368366a526885aa152f05836 to your computer and use it in GitHub Desktop.
set default browser on macos from command line #snippet
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# with help from https://gist.github.com/miketaylr/5969656 | |
import sys | |
try: | |
from LaunchServices import ( | |
LSSetDefaultHandlerForURLScheme, | |
LSCopyDefaultHandlerForURLScheme, | |
) | |
except: | |
sys.exit( | |
"to run this script, first run 'pip install pyobjc-framework-LaunchServices'" | |
) | |
supported_browser_ids = { | |
"chrome": "com.google.chrome", | |
"firefox": "org.mozilla.firefox", | |
"safari": "com.apple.Safari", | |
} | |
supported_browsers = supported_browser_ids.keys() | |
try: | |
browser = sys.argv[1] | |
browser_id = supported_browser_ids[browser] | |
except: | |
sys.exit("Usage: {} <{}>".format(sys.argv[0], "|".join(supported_browsers))) | |
url_schemes = ["http", "https"] | |
# kLSRolesViewer | |
# see https://developer.apple.com/library/mac/#documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/c/tdef/LSRolesMask | |
rolesViewer = 0x00000002 | |
is_updated = False | |
for scheme in url_schemes: | |
current_handler = LSCopyDefaultHandlerForURLScheme(scheme) | |
if current_handler != browser_id: | |
is_updated = True | |
LSSetDefaultHandlerForURLScheme(scheme, browser_id) | |
if is_updated: | |
print( | |
"Click 'Use {}' to set the default browser to {}".format( | |
browser.title(), browser | |
) | |
) | |
else: | |
print("{} is already the default browser".format(browser)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment