February 2012

Find Safari Tabs with AppleScript

If you sometimes find yourself with so many tabs in Safari that you can't find things and end up opening them again in a new tab, this script can help.

It asks for a search term, which it matches against the name (title) of every open tab, as well as the URL. If it finds one match, it raises that window to the top and switches to that tab. If it finds multiple matches, it offers you a list of matches that you can select from.

set question to display dialog ("Find Safari tab:") default answer ""

set searchpat to text returned of question


tell application "Safari"

set winlist to every window

set winmatchlist to {}

set tabmatchlist to {}

set tabnamematchlist to {}

repeat with win in winlist

set ok to true

try

set tablist to every tab of win

on error errmsg

--display dialog name of win as string

set ok to false

end try

if ok 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

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

else if (count of tabmatchlist) = 0 then

display dialog "No matches"

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

end if

end if

end tell

To run this, open up Look in Applications, then Utilities, and run "AppleScript Editor". Cut and paste the program into a new window there and save it as "Find Safari Tab" (or whatever name you like), and place it in your home directory, under Library/Scripts. Then in the AppleScript Editor Preferences, check the box "Show Script menu in menu bar (I also uncheck "Show Computer scripts", which would show lots of system scripts you might not want). You'll get a script menu in your menubar with your new script in it. Just select it to run it.

This script has a younger brother which does roughly the same thing for Terminal windows and tabs: Find Terminal Sessions.

And if you're wondering how I converted the AppleScript Editor formatted program into HTML, I also do that with software. I can convert any copied text that has formatting into HTML.


Reader Comments (Experimental. Moderated, expect delays. Posts may be edited or ignored. I reserve the right to remove any or all comments, at any time.)

No comments

Add a comment


More Mac OS X Stuff


Tom Fine's Home
Send Me Email