Skip to content

Instantly share code, notes, and snippets.

@masnick
Last active December 17, 2017 19:24
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 masnick/380d067da1f521b01cb0 to your computer and use it in GitHub Desktop.
Save masnick/380d067da1f521b01cb0 to your computer and use it in GitHub Desktop.
Find FastMail tab(s) in Safari
-- original from http://hea-www.harvard.edu/~fine/OSX/safari-tabs.html
-- Last updated January 16, 2015 1:22 PM
set searchpat to "fastmail.com"
tell application "Safari"
activate
set winlist to every window
set winmatchlist to {}
set tabmatchlist to {}
set tabnamematchlist to {}
repeat with win in winlist
set numtabs to 0
try
set tablist to every tab of win
set numtabs to length of tablist
end try
if numtabs is greater than 0 then
repeat with t in tablist
--if searchpat is in (name of t as string) then
--set end of winmatchlist to win
--set end of tabmatchlist to t
--set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ". " & (name of t as string)
--display dialog name of t as string
--else if searchpat is in (URL of t as string) then
if searchpat is in (URL of t as string) then
set end of winmatchlist to win
set end of tabmatchlist to t
set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ". " & (name of t as string)
--display dialog name of t as string
end if
end repeat
end if
end repeat
if (count of tabmatchlist) = 1 then
--display dialog "one!"
set w to (item 1 of winmatchlist)
set t to item 1 of tabmatchlist
set current tab of w to t
set index of w to 1
-- hack to make the window actually come to the front: http://stackoverflow.com/questions/5682413/how-do-i-make-a-safari-window-active-using-applescript-elegantly
set visible of w to false
set visible of w to true
else if (count of tabmatchlist) = 0 then
make new document with properties {URL:"https://www.fastmail.com/mail/Inbox/"}
else
set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
set AppleScript's text item delimiters to "."
if whichtab is not equal to false then
set tmp to text items of (whichtab as string)
set w to (item 1 of tmp) as integer
set t to (item 2 of tmp) as integer
set current tab of window id w to tab t of window id w
set index of window id w to 1
set visible of window id w to false
set visible of window id w to true
end if
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment