Set default web browser via command line?

Zettt picture Zettt · Jul 8, 2013 · Viewed 13.9k times · Source

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.

Answer

Florian Wendelborn picture Florian Wendelborn · May 9, 2018

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)