From c891a7760ec5ea7f76452759d5eff879874a87c1 Mon Sep 17 00:00:00 2001 From: Paul Holt Date: Tue, 16 Jun 2026 14:25:10 +1000 Subject: [PATCH] add -s flag to print current default browser silently Outputs just the browser name with no list or asterisk, useful for scripting. --- CLAUDE.md | 34 ++++++++++++++++++++++++++++++++++ src/main.m | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..5243c81 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,34 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## What this is + +`defaultbrowser` is a macOS command-line tool (Objective-C) that reads and sets the default HTTP/HTTPS handler using the macOS Launch Services API. The entire implementation lives in `src/main.m`. + +## Build commands + +```sh +make # build the binary +make install # install to /usr/local/bin (override with PREFIX=...) +make clean # remove the binary +``` + +The binary is compiled directly with `gcc` (or `clang` via the `CC` variable) against the `Foundation` and `ApplicationServices` frameworks. There is no Xcode project. + +## Architecture + +All logic is in `src/main.m`: + +- `get_http_handlers()` — calls `LSCopyAllHandlersForURLScheme("http")` and returns a dict mapping short name → bundle ID (e.g. `"chrome"` → `"com.google.chrome"`). +- `get_current_http_handler()` — calls `LSCopyDefaultHandlerForURLScheme("http")` and returns the short name. +- `set_default_handler()` — calls `LSSetDefaultHandlerForURLScheme` for both `http` and `https`. +- `main()` — with no args, lists all handlers (marking the current one with `*`); with one arg, sets that browser as default. + +Bundle ID → short name conversion strips everything before the last `.` and lowercases it (`app_name_from_bundle_id`). Browser name matching uses `caseInsensitiveCompare`. + +## macOS-specific notes + +- Requires macOS. Launch Services functions (`LS*`) are in `ApplicationServices.framework`. +- `LSCopyAllHandlersForURLScheme` is deprecated on macOS 12+ but still functional. Future macOS versions may break it. +- Setting the default browser may trigger a system confirmation dialog on newer macOS versions. diff --git a/src/main.m b/src/main.m index 020bddb..9e0a9b8 100644 --- a/src/main.m +++ b/src/main.m @@ -58,6 +58,8 @@ int main(int argc, const char *argv[]) { char *mark = [key caseInsensitiveCompare:current_handler_name] == NSOrderedSame ? "* " : " "; printf("%s%s\n", mark, [key UTF8String]); } + } else if (strcmp(target, "-s") == 0) { + printf("%s\n", [current_handler_name UTF8String]); } else { NSString *target_handler_name = [NSString stringWithUTF8String:target];