Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Last active April 5, 2025 19:57
Show Gist options
  • Save johnlindquist/8dfb7d7e1b087bebfb2c15f055b69c00 to your computer and use it in GitHub Desktop.
Save johnlindquist/8dfb7d7e1b087bebfb2c15f055b69c00 to your computer and use it in GitHub Desktop.
Focus-Tab: AppleScript utility for opening or focusing browser tabs from the command line

Focus-Tab: Browser Tab Manager for macOS

Overview

Focus-Tab is an AppleScript utility that allows you to quickly open or switch to specific websites in your browser from the command line. It's particularly useful for:

  • Workflow automation
  • Quick navigation to frequently visited sites
  • Scripts and shell aliases for browser-based tools
  • Integration with other automation tools

Features

  • Chrome-specific tab focusing: When used with Google Chrome, the script will search for existing tabs containing the provided URL and focus them instead of opening duplicate tabs
  • Cross-browser support: Works with any browser on macOS (Safari, Firefox, Brave, etc.)
  • Simple command-line interface: Just provide a URL and optionally specify the browser

Usage

osascript focus-tab.scpt <url> [browser]

Examples:

# Open or focus Google in Chrome (default browser)
osascript focus-tab.scpt google.com

# Open GitHub in Safari
osascript focus-tab.scpt github.com "Safari"

# Open YouTube in Brave Browser
osascript focus-tab.scpt youtube.com "Brave Browser"

How It Works

The script has two different behaviors depending on the browser:

  1. For Google Chrome: The script will first search through all open windows and tabs for any URL containing the search term. If found, it will focus that tab instead of opening a new one.

  2. For other browsers: The script will simply open the URL in a new tab (the tab-finding feature is Chrome-specific due to AppleScript compatibility limitations).

Use Cases

  • Create shell aliases for frequently visited sites
  • Use as part of more complex automation workflows
  • Integrate with hotkeys or keyboard shortcut tools
  • Control browser tabs from other applications or scripts

Installation

Simply save the focus-tab.scpt file to a convenient location on your macOS system.

For easier access, you might want to:

  1. Place it in a scripts directory
  2. Create a shell alias in your .zshrc or .bashrc file
  3. Integrate with tools like Alfred, Raycast, or Keyboard Maestro
-- focus-tab.scpt
-- AppleScript to open or focus tabs in a browser
-- Usage: osascript focus-tab.scpt <url> [browser]
--
-- For Chrome: Attempts to find and focus existing tabs with matching URLs
-- For other browsers: Simply opens the URL in a new tab
on run argv
set targetURL to item 1 of argv
set browserName to "Google Chrome"
if (count of argv) > 1 then
set browserName to item 2 of argv
end if
tell application browserName
activate
end tell
-- Make sure the browser is frontmost
tell application "System Events"
tell process browserName
set frontmost to true
end tell
end tell
-- Special handling for Chrome - try to find existing tabs
if browserName is "Google Chrome" then
set foundTab to false
tell application "Google Chrome"
-- First check if Chrome has any windows
if (count of windows) > 0 then
-- Prepare URL for comparison without http/https
set searchTerm to targetURL
-- Try to find and focus an existing tab
repeat with w in windows
try
set tabIndex to 1
repeat with t in tabs of w
try
set tabUrl to URL of t
if tabUrl contains searchTerm then
set active tab index of w to tabIndex
set index of w to 1
set foundTab to true
exit repeat
end if
set tabIndex to tabIndex + 1
on error
-- Skip any tab that causes an error
set tabIndex to tabIndex + 1
end try
end repeat
if foundTab then exit repeat
on error
-- Skip any window that causes an error
end try
end repeat
if foundTab then
return "Focused existing tab with URL containing: " & targetURL
end if
end if
-- If we reach here, we didn't find a matching tab
-- Format URL and open it
if targetURL does not start with "http" then
set targetURL to "https://" & targetURL
end if
open location targetURL
end tell
else
-- For other browsers, just open the URL
tell application browserName
if targetURL does not start with "http" then
set targetURL to "https://" & targetURL
end if
open location targetURL
end tell
end if
return targetURL
end run

Development Challenges

When creating this script, we encountered several significant challenges that are worth noting for anyone who might want to modify or extend it:

AppleScript Browser Interaction Limitations

  1. Property Access Errors: We consistently encountered -2740 errors ("A property can't go after this identifier") when trying to access tab properties in browsers. This is why the Chrome-specific tab-finding approach is structured with careful error handling around each property access.

  2. Syntax Sensitivity: AppleScript's syntax for error handling and property access within browser automation is extremely sensitive. Minor changes in structure would often result in -2741 errors ("Expected end of line but found identifier").

  3. Browser Security Restrictions: Modern browsers have increasingly restricted what AppleScript can access, making consistent tab property access challenging.

Implementation Compromises

After multiple iterations, we made several design decisions:

  1. Chrome-Specific Focusing: The tab-finding feature only works reliably in Chrome. We tried similar approaches with Safari and other browsers but encountered too many inconsistencies.

  2. Error Handling Strategy: The script uses nested try/catch blocks to handle errors at different levels (window access, tab enumeration, URL property access) rather than failing completely.

  3. Simplicity Over Comprehensiveness: For non-Chrome browsers, we opted for a simpler approach (just opening the URL) rather than trying to implement unreliable tab-finding logic.

These limitations explain why the script behavior differs between Chrome and other browsers, and why we recommend using it primarily with Chrome for the tab-focusing functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment