32

I'm trying to figure out how to set the default web browser on OS X via command line. I found this forum post, but the solution they are working out there is to open a specific URL with a specific application. I am looking for a way to set the default browser system wide. There's this neat app on GitHub, named Objektiv, which does exactly what I am after, but it's an app. There's a Cocoa method, apparently, to set the NSWorkSpace default browser.

The preference file com.apple.LaunchServices needs to be changed for this, maybe more.

How to set a different default browser via command line?

Thanks for help.

Zettt
  • 879
  • 2
  • 8
  • 27
  • 7
    I was quite frustrated by that missing functionality, so I built something quickly: https://github.com/kerma/defaultbrowser. I haven't really tested it besides my own computer, but it may help you as well. – kerma Jul 01 '14 at 20:51
  • Thanks for this little app...it perfectly solved my similar problem and I can now switch defaultbrowsers using Keyboard Maestro! – Chris Sep 06 '14 at 19:37
  • Approximate cross-site duplicate: http://apple.stackexchange.com/questions/144589/how-can-i-set-the-default-browser-depending-on-time-of-day – tripleee Mar 21 '17 at 04:20
  • @kerma I get `defaultbrowser brave brave is not available as an HTTP handler` on Monterey – volvox Apr 04 '23 at 11:46
  • Ignore that - looks like `brew install defaultbrowser` is the way to go. If you want Brave, use `defaultbrowser browser` – volvox Apr 04 '23 at 11:49

4 Answers4

33

I found this tool. After installing it, you can do this:

defaultbrowser chrome

Here's the entire source-code in case the link 404s in the future. It is licensed under the MIT license with Copyright (c) 2014 Margus Kerma.

#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>

NSString* app_name_from_bundle_id(NSString *app_bundle_id) {
    return [[app_bundle_id componentsSeparatedByString:@"."] lastObject];
}

NSMutableDictionary* get_http_handlers() {
    NSArray *handlers =
      (__bridge NSArray *) LSCopyAllHandlersForURLScheme(
        (__bridge CFStringRef) @"http"
      );

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    for (int i = 0; i < [handlers count]; i++) {
        NSString *handler = [handlers objectAtIndex:i];
        dict[[app_name_from_bundle_id(handler) lowercaseString]] = handler;
    }

    return dict;
}

NSString* get_current_http_handler() {
    NSString *handler =
        (__bridge NSString *) LSCopyDefaultHandlerForURLScheme(
            (__bridge CFStringRef) @"http"
        );

    return app_name_from_bundle_id(handler);
}

void set_default_handler(NSString *url_scheme, NSString *handler) {
    LSSetDefaultHandlerForURLScheme(
        (__bridge CFStringRef) url_scheme,
        (__bridge CFStringRef) handler
    );
}

int main(int argc, const char *argv[]) {
    const char *target = (argc == 1) ? '\0' : argv[1];

    @autoreleasepool {
        // Get all HTTP handlers
        NSMutableDictionary *handlers = get_http_handlers();

        // Get current HTTP handler
        NSString *current_handler_name = get_current_http_handler();

        if (target == '\0') {
            // List all HTTP handlers, marking the current one with a star
            for (NSString *key in handlers) {
                char *mark = [key isEqual:current_handler_name] ? "* " : "  ";
                printf("%s%s\n", mark, [key UTF8String]);
            }
        } else {
            NSString *target_handler_name = [NSString stringWithUTF8String:target];

            if ([target_handler_name isEqual:current_handler_name]) {
              printf("%s is already set as the default HTTP handler\n", target);
            } else {
                NSString *target_handler = handlers[target_handler_name];

                if (target_handler != nil) {
                    // Set new HTTP handler (HTTP and HTTPS separately)
                    set_default_handler(@"http", target_handler);
                    set_default_handler(@"https", target_handler);
                } else {
                    printf("%s is not available as an HTTP handler\n", target);

                    return 1;
                }
            }
        }
    }

    return 0;
}

Makefile:

BIN ?= defaultbrowser
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin

CC = gcc
CFLAGS = -O2

.PHONY: all install uninstall clean

all:
    gcc -o $(BIN) $(CFLAGS) -framework Foundation -framework ApplicationServices src/main.m

install: all
    install -d $(BINDIR)
    install -m 755 $(BIN) $(BINDIR)

uninstall:
    rm -f $(BINDIR)/$(BIN)

clean:
    rm -f $(BIN)
Florian Wendelborn
  • 1,667
  • 1
  • 19
  • 29
10

I know this doesn't specifically answer your question, but if you only need to do this for Chrome, it has a flag that let's you set itself as the default browser:

$ open -a "Google Chrome" --args --make-default-browser
bryan kennedy
  • 6,969
  • 5
  • 43
  • 64
5

For newer versions of macOS (Catalina, Big Sur) I re-implemented the default browser tool in Swift (called it defbro). Main reason was to use a "modern" programming language that gives me an easy way to tweak and improve it. I also added json output defbro --json, in case you want to do other stuff with it.

Installation: brew install jwbargsten/misc/defbro

Repo: https://github.com/jwbargsten/defbro

contramao
  • 51
  • 1
  • 2
-11

The only way is to set it in the main Settings:

  1. From the Apple menu, choose System Preferences, then click General.
  2. Click the “Default web browser” pop-up menu and choose a web browser, like Chrome.
Raja Rao
  • 5,691
  • 2
  • 28
  • 31