Focus an existing browser tab in the macOS default browser, or open it if the tab is missing.
./focus-browser-tab https://mail.google.com/This repo is intentionally just a script. No npm package, no install step beyond putting the file somewhere in your PATH.
Download the script:
curl -fsSL https://raw.githubusercontent.com/koenbok/FocusBrowserTab/main/focus-browser-tab -o focus-browser-tab
chmod +x focus-browser-tabInstall it globally:
sudo mv focus-browser-tab /usr/local/bin/focus-browser-tabThen use it like this:
focus-browser-tab https://mail.google.com/
focus-browser-tab mail.google.com
focus-browser-tab --app "Google Chrome Beta" https://linear.app/
FOCUS_BROWSER_TAB_APP="Arc" focus-browser-tab https://linear.app/- Defaults to the macOS default browser for
https:URLs - Accepts an explicit browser via
--apporFOCUS_BROWSER_TAB_APP - Normalizes a host like
mail.google.comtohttps://mail.google.com - If the browser is not already running, opens the target URL
- If the browser is running, scans existing tabs via AppleScript
- Focuses the first matching tab by host and path prefix
- Opens a new tab only when no existing tab matches
- Hosts match exactly or by subdomain
- Paths match exactly or by prefix
- URL fragments are ignored
- Query strings are ignored for matching
Examples:
https://mail.google.com/matcheshttps://mail.google.com/mail/u/0/#inboxhttps://app.slack.com/clientmatcheshttps://app.slack.com/client/T123/C456
- macOS
- macOS default browser should expose a Chrome-compatible AppleScript dictionary
- Or pass
--appto target a specific scriptable Chromium-based browser
- The script uses
osascriptand is macOS-only. - By default the browser target is resolved from macOS via
NSWorkspace. - Some browsers expose similar AppleScript dictionaries, but compatibility is not guaranteed.