Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set default browser on OSX with Python
#/usr/bin/env python
from LaunchServices import LSSetDefaultHandlerForURLScheme
from LaunchServices import LSSetDefaultRoleHandlerForContentType
# 0x00000002 = kLSRolesViewer
# see https://developer.apple.com/library/mac/#documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/c/tdef/LSRolesMask
LSSetDefaultRoleHandlerForContentType("public.html", 0x00000002, "com.operasoftware.operanext")
LSSetDefaultRoleHandlerForContentType("public.xhtml", 0x00000002, "com.operasoftware.operanext")
LSSetDefaultHandlerForURLScheme("http", "com.operasoftware.operanext")
LSSetDefaultHandlerForURLScheme("https", "com.operasoftware.operanext")
@miketaylr

This comment has been minimized.

Copy link
Owner Author

miketaylr commented Jul 10, 2013

And then to read it back:

>>> from LaunchServices import LSCopyDefaultRoleHandlerForContentType
>>> LSCopyDefaultRoleHandlerForContentType("public.html", 0x000000002)
u'com.operasoftware.operanext'
>>> from LaunchServices import LSCopyDefaultHandlerForURLScheme
>>> LSCopyDefaultHandlerForURLScheme("http")
u'com.operasoftware.operanext'
>>> 
@oschrenk

This comment has been minimized.

Copy link

oschrenk commented Jul 23, 2013

Awesome.

Could you clarify how to read the values back? I'm not familiar with Python and have some troubles

#/usr/bin/env python
from LaunchServices import LSCopyDefaultRoleHandlerForContentType
from LaunchServices import LSCopyDefaultHandlerForURLScheme

# 0x00000002 = kLSRolesViewer
# see https://developer.apple.com/library/mac/#documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/c/tdef/LSRolesMask
print LSCopyDefaultRoleHandlerForContentType ("public.html", 0x000000002)
print LSCopyDefaultHandlerForURLScheme("http")

I get

from: can't read /var/mail/LaunchServices
from: can't read /var/mail/LaunchServices
./get-default-browser.py: line 7: syntax error near unexpected token `('
./get-default-browser.py: line 7: `role = LSCopyDefaultRoleHandlerForContentType ("public.html", 0x000000002)'
@miketaylr

This comment has been minimized.

Copy link
Owner Author

miketaylr commented Aug 28, 2013

Oh hey @oschrenk -- didn't see this until just now. Did you figure it out? It looks like you've got a space between the method and paren in LSCopyDefaultRoleHandlerForContentType ("public.

@fatuhoku

This comment has been minimized.

Copy link

fatuhoku commented Oct 16, 2013

Heya, is there a way to do this without using PyObjC? I'd like to package my application as a cross-platform app so I want as few platform-specific dependencies as possible.

@quedayone

This comment has been minimized.

@thopiekar

This comment has been minimized.

Copy link

thopiekar commented Oct 25, 2018

@miketaylr: About your snippet to get the handler of an extension:
Is there a way to get the installation path of an handler?

For example: I want to know where the executable of OpenSCAD is. All I know is the file extension. With your snippet I would know the name of the handler, but I need to know where the executable is, so I can run a CLI command.

Thanks!

@wolfpat01

This comment has been minimized.

Copy link

wolfpat01 commented Oct 31, 2019

is there any way to make your python program work as a default browser and whenever there is a specific URL lunches chrome or firefox

@imigivanov

This comment has been minimized.

Copy link

imigivanov commented Jun 28, 2020

is there any way to make your python program work as a default browser and whenever there is a specific URL lunches chrome or firefox

@wolfpat01 You have probably found out this already, but for other people who might be interested in the same thing: this can be achieved by using Hammerspoon with URLDispatcher extension. Not related to a Python script, but might be useful for some people to know. In addition to that, URLs there can be configured in a flexible manner with the help of regular expressions. Please pay attention that once configures you will need to set Hammerspoon as your default browser for using the aforementioned extension properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.