Programmatically Changing the Mac OS X Browser

I am starting to get a lot questions on how to change the default browser for Mac OS X. I don’t know if it’s in preparation for summer imaging or if it’s because people clicked the button to reset their default browser to Chrome and then realized that, while fast, it just isn’t Safari. Either way, the default browser is stored in the com.apple.LaunchServices.plist, inside the LSHandlers array. Now, changing this with a defaults command would involve copying all the information into said command and then dumping it back in after changing all entries of com.apple.safari to the information for the browser you actually want to use. If you do that, look for the following keys:

LSHandlerRoleAll = “com.apple.safari”;

LSHandlerRoleAll = “com.apple.safari”;

LSHandlerRoleViewer = “com.apple.safari”;

LSHandlerRoleViewer = “com.apple.safari”;

LSHandlerRoleAll = “com.apple.safari”;
LSHandlerRoleAll = “com.apple.safari”;
LSHandlerRoleViewer = “com.apple.safari”;
LSHandlerRoleViewer = “com.apple.safari”;

These may say something like com.mozilla.firefox for now and you want to change them to something different, like com.google.chrome. Either way, you can do it by augmenting the entire contents of the array, which would replace the contents of the file or using PlistBuddy, which could be used to more granularly alter each setting.

8 Comments

  • Sam Bergin
    December 12, 2009 - 10:39 am | Permalink

    I know you’re into doing stuff the CLI way and all, but wouldn’t it be easier to change the default browser in Safari Preferences, and then just grab a copy of the updated plist file (for imaging, I mean)? Maybe I’m missing something.

    • admin
      December 12, 2009 - 12:43 pm | Permalink

      Great question! Yes, it would be way easier to edit the Safari Preferences in their respective screens and absolutely one of the better ways to do that in a monolithic type of imaging environment. But, what if the user has existing settings and you want to send them only one setting through ARD without messing up the rest of their Safari settings? Or if you want to edit just one setting per payload-free package so you can have a package for each bullet in a click matrix or something like that. Lots of reasons to do it one way or the other. Depends on each persons imaging strategy, whether you can do something through a managed preference and stuff like that…

    • admin
      December 12, 2009 - 12:43 pm | Permalink

      Again, love the question!

  • Sam Bergin
    December 12, 2009 - 2:23 pm | Permalink

    Ah, excellent point Charles. Thanks for the insight. I hadn’t thought of the need to edit an existing up-and-running user’s preferences…

  • admin
    December 12, 2009 - 3:28 pm | Permalink

    Next up, how to write bookmarks into an existing users Safari Bookmarks in order.

  • elyse
    January 13, 2010 - 8:57 am | Permalink

    Hey, I found this on Google, kind of randomly, but I have a question. Is there a way (somewhere in that file) to set one browser to be default for one particular program? I use Firefox at work for all work-related web development things, and I want it to be default, except in my chat program (Adium) I want to open links from that (since they are non work related) in Chrome. :)

    • January 13, 2010 - 10:24 am | Permalink

      I’m afraid that would have to be done by the application that invokes the browser. If Adium had a feature that allowed you to override the OS default browser then it would work. That can be coded into any application, so if it’s not there I’d do a feature request on the Adium site. Sorry I’m not much help, but great question.

  • TGB
    April 4, 2011 - 12:43 am | Permalink

    One option: RCDefaultApp includes a hidden command-line utility called lstool buried in its Resources, which appears to be self-contained:

    ./lstool read internet Web

    ./lstool write internet Web /Applications/Safari.app

  • Comments are closed.