Set default web browser via command line?
Asked Answered
T

4

34

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.

Top answered 8/7, 2013 at 14:5 Comment(5)
I was quite frustrated by that missing functionality, so I built something quickly: github.com/kerma/defaultbrowser. I haven't really tested it besides my own computer, but it may help you as well.Mutate
Thanks for this little app...it perfectly solved my similar problem and I can now switch defaultbrowsers using Keyboard Maestro!Photochronograph
Approximate cross-site duplicate: apple.stackexchange.com/questions/144589/…Lethargy
@Mutate I get defaultbrowser brave brave is not available as an HTTP handler on MontereyMaimonides
Ignore that - looks like brew install defaultbrowser is the way to go. If you want Brave, use defaultbrowser browserMaimonides
H
34

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)
Heresiarch answered 9/5, 2018 at 21:58 Comment(2)
You can also install this via homebrew now: brew install defaultbrowserIdiotism
Thanks; this worked perfectly! My default browser somehow got unset in Preferences, and this fixed it.Brandwein
F
11

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
Fredelia answered 18/2, 2014 at 20:44 Comment(1)
Can we do this for opera?Tarratarradiddle
S
6

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

Scrivens answered 25/5, 2021 at 6:52 Comment(2)
Thank you, I have tried this and works as advertised! You still need to confirm the change though, but this can also be automated using applescript: felixparadis.com/posts/…Jericajericho
Doesn't work with 14.1.2Stowaway
P
-12

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.
Pirozzo answered 10/4, 2017 at 22:14 Comment(1)
Requested ▾ Comment: What @DanRosenstark said: command_line != GUIBlackfellow

© 2022 - 2024 — McMap. All rights reserved.