Created
March 3, 2026 11:23
-
-
Save torgeir/ae41a3bbbc85c13119e401375c3a5082 to your computer and use it in GitHub Desktop.
BrowserRouter: A macos application to route specific urls to a given Firefox container.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| APP_DIR="$HOME/Applications/BrowserRouter.app" | |
| # Compile the AppleScript into an application | |
| osacompile -o "$APP_DIR" << 'APPLESCRIPT' | |
| use AppleScript version "2.4" | |
| use scripting additions | |
| property routingRules : {¬ | |
| {"home.atlassian.com", "Work" }, ¬ | |
| {"selvbetjening.dfo.no", "Work" } ¬ | |
| } | |
| property fallbackContainer : "Personal" | |
| property firefoxApp : "Firefox Developer Edition" | |
| on open location theURL | |
| routeURL(theURL) | |
| end open location | |
| on open theItems | |
| repeat with anItem in theItems | |
| set itemStr to anItem as text | |
| if itemStr starts with "http" or itemStr starts with "https" then | |
| routeURL(itemStr) | |
| else | |
| try | |
| set itemStr to POSIX path of anItem | |
| routeURL(itemStr) | |
| end try | |
| end if | |
| end repeat | |
| end open | |
| on run | |
| display dialog ¬ | |
| "BrowserRouter is running." & return & return & ¬ | |
| "This app intercepts URLs and routes them to Firefox containers." & return & return & ¬ | |
| "Set it as your default browser in:" & return & ¬ | |
| "System Settings → Desktop & Dock → Default web browser" ¬ | |
| buttons {"OK"} default button "OK" with title "BrowserRouter" with icon note | |
| end run | |
| on routeURL(theURL) | |
| set theURL to trimText(theURL) | |
| set targetContainer to fallbackContainer | |
| repeat with aRule in routingRules | |
| if theURL contains (item 1 of aRule) then | |
| set targetContainer to item 2 of aRule | |
| exit repeat | |
| end if | |
| end repeat | |
| log "BrowserRouter: " & theURL & " → Container: " & targetContainer | |
| set containerURL to "ext+container:name=" & urlEncode(targetContainer) & "&url=" & urlEncode(theURL) | |
| try | |
| do shell script "open -a " & quoted form of firefoxApp & " -- " & quoted form of containerURL | |
| on error errMsg | |
| display dialog "Error routing URL:" & return & errMsg & return & return & ¬ | |
| "Opening directly instead." buttons {"Cancel", "Open Directly"} default button "Open Directly" with icon caution | |
| do shell script "open -a " & quoted form of firefoxApp & " -- " & quoted form of theURL | |
| end try | |
| end routeURL | |
| on urlEncode(theText) | |
| return do shell script "python3 -c 'import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1], safe=\"\"))' " & quoted form of theText | |
| end urlEncode | |
| on trimText(theText) | |
| return do shell script "echo " & quoted form of theText & " | xargs" | |
| end trimText | |
| APPLESCRIPT | |
| # Fix the Info.plist — osacompile doesn't add URL scheme handling | |
| PLIST="$APP_DIR/Contents/Info.plist" | |
| EXECUTABLE=$(/usr/libexec/PlistBuddy -c "Print :CFBundleExecutable" "$PLIST") | |
| # Register http and https | |
| /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.local.browserrouter" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes array" "$PLIST" 2>/dev/null || true | |
| /usr/libexec/PlistBuddy -c "Delete :CFBundleURLTypes" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes array" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0 dict" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLName string 'HTTP URL'" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleTypeRole string Viewer" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes array" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string http" "$PLIST" | |
| /usr/libexec/PlistBuddy -c "Add :CFBundleURLTypes:0:CFBundleURLSchemes:1 string https" "$PLIST" | |
| # Clear quarantine and register as browser | |
| xattr -cr "$APP_DIR" | |
| /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$APP_DIR" | |
| echo "" | |
| echo "Run:" | |
| echo " chmod u+x install-browser-router.sh && ./install-browser-router.sh" | |
| echo "Installed to: $APP_DIR" | |
| echo "Executable: $EXECUTABLE" | |
| echo "Bundle ID: $(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$PLIST")" | |
| echo "" | |
| echo -e "Test with default browser:" | |
| echo -e " open https://github.com" | |
| echo -e " open https://selvbetjening.dfo.no" | |
| echo -e "Test with this application:" | |
| echo -e " open -b com.local.browserrouter https://selvbetjening.dfo.no" | |
| echo "" | |
| echo "Then set as default browser in:" | |
| echo " System Settings → Desktop & Dock → Default web browser" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment