mathiasbynens / dotfiles

:wrench: .files, including ~/.macos — sensible hacker defaults for macOS
https://mths.be/dotfiles
MIT License
30.2k stars 8.74k forks source link

Set default browser #154

Open sindresorhus opened 11 years ago

sindresorhus commented 11 years ago

Is it possible to programmatically set the default browser?

Either through a Plist or AppleScript or something.

Would be useful to have in .osx

sapegin commented 11 years ago

Chrome has command line argument for this: --make-default-browser (example). I’m not sure it’s possible for other browsers.

sindresorhus commented 11 years ago

Neat, but that doesn't solve it for wanting Chrome Canary, Firefox or any other browser as default.

mathiasbynens commented 11 years ago

@sapegin: Thanks for the info! I didn’t know about that Chrome flag.


I did some digging, and it seems defaults read com.apple.LaunchServices LSHandlers returns an array containing entries such as the following (assuming Google Chrome Canary is the default browser):

        {
        LSHandlerContentType = "public.html";
        LSHandlerRoleAll = "com.google.chrome.canary";
    },
        {
        LSHandlerContentType = "public.xhtml";
        LSHandlerRoleAll = "com.google.chrome.canary";
    },
        {
        LSHandlerRoleAll = "com.google.chrome.canary";
        LSHandlerURLScheme = http;
    },
        {
        LSHandlerRoleAll = "com.google.chrome.canary";
        LSHandlerURLScheme = https;
    }

As there are lots of other entries though, it’s kinda tricky to add/modify these values cleanly using the defaults command. However, this could easily be done using duti (ref. #54) using a settings file like this:

com.google.chrome.canary public.html all
com.google.chrome.canary public.xhtml all
com.google.chrome.canary http
com.google.chrome.canary https

(Untested, but it should work.)

To set another browser as the default, simply look up its bundle ID and use it instead of com.google.chrome.canary.

kevinSuttle commented 11 years ago

@sindresorhus You can't make Chrome Canary your default browser. Just a heads up.

sindresorhus commented 11 years ago

@kevinSuttle :( I don't understand the restriction...

kevinSuttle commented 11 years ago

@sindresorhus It's apparently so it can be ran side by side with the stable builds.

http://www.google.com/intl/en/chrome/browser/canary.html (Bottom right: Side by Side Install).

sindresorhus commented 11 years ago

@kevinSuttle you can actually set Chrome Canary as default, just not programatically.

Go into Safari and set it there. Works great for me. But would be awesome to have this preconfigured in my dotfiles.

kevinSuttle commented 11 years ago

@sindresorhus Hm. I did not know that. Thanks! :thumbsup:

miketaylr commented 11 years ago

I just wrote the following to do this in Python: https://gist.github.com/miketaylr/5969656

mathiasbynens commented 11 years ago

@miketaylr Awesome! Nice work.