我试图弄清楚如何通过命令行在 OS X 上设置默认网络浏览器。我找到了这个论坛帖子,但他们正在制定的解决方案是使用特定应用程序打开特定 URL。我正在寻找一种设置默认浏览器系统范围的方法。GitHub 上有一个名为Objektiv的简洁应用程序,它完全符合我的要求,但它是一个应用程序。显然,有一个 Cocoa 方法来设置 NSWorkSpace 默认浏览器。
com.apple.LaunchServices为此需要更改首选项文件,也许更多。
如何通过命令行设置不同的默认浏览器?
感谢帮助。
我找到了这个工具。安装后,您可以这样做:
defaultbrowser chrome
这是完整的源代码,以防404将来出现链接。它在 MIT 许可下获得许可,版权所有 (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;
}
生成文件:
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)
我知道这并没有具体回答您的问题,但如果您只需要为 Chrome 执行此操作,它有一个标志可以让您将自己设置为默认浏览器:
$ open -a "Google Chrome" --args --make-default-browser
对于较新版本的 macOS(Catalina、Big Sur),我在 Swift 中重新实现了默认浏览器工具(称为defbro)。主要原因是使用一种“现代”编程语言,它让我可以轻松地调整和改进它。我还添加了 json output defbro --json,以防你想用它做其他事情。
安装:brew install jwbargsten/misc/defbro
唯一的方法是在主设置中进行设置: