Skip to content

Instantly share code, notes, and snippets.

@stevenschobert
Created June 1, 2016 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenschobert/ba6845feb5b0db7cdf04d04362720424 to your computer and use it in GitHub Desktop.
Save stevenschobert/ba6845feb5b0db7cdf04d04362720424 to your computer and use it in GitHub Desktop.
AppleScript to find existing tab with URL, or open a new one.
tell application "Safari"
set topWindows to every window whose name is not ""
set numWindows to the number of topWindows
set didFind to false
set targetUrl to "http://localhost:3000/"
repeat with x from 1 to numWindows
set numTabs to the number of tabs in window x
repeat with y from 1 to numTabs
set tabUrl to the URL of tab y of window x
if tabUrl contains targetUrl then
set didFind to true
tell window x to set current tab to tab y
end if
end repeat
end repeat
if didFind is false then
tell window 1 to set current tab to (make new tab with properties {URL:targetUrl})
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment