Bring a Safari tab to front by name

How can I use Keyboard Maestro to bring a Safari tab to front by name?

Googling around, I found this thread which explains how to select a Chrome tab by name, but I can't seem to find a solution for Safari.

This works for me:

45-pty-fs8
Bring Named Safari Tab to Front.kmmacros (3.3 KB)

In the first action (Set Variable) you have to enter the name of the tab you want to bring to the front; can also be a part of the name, case insensitive.

The script should go through all open Safari windows and give you the first tab where the name matches.

This is a first draft of an AppleScript, so there is certainly much room for improvement.


Edit 2020-01-30:

  • Replaced the Set Variable action with a User Prompt action
2 Likes

I have no idea now where the script in this macro came from—certainly not me since I'm not sufficiently skilled—but here's the same idea for a macro that I use. Maybe it's of use to someone.

image

The AppleScript code:

	set searchpat to (process tokens "%Variable%Find%")
end tell

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 "Multiple matches. 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
1 Like

@big_smile

I find the script @NaOH has posted is far superior to mine, because

  • it gives you a list of matches, if there are more than one
  • it also searches the URLs, not only the names of tabs

PS: To make my script look a little less poor, I’ve added a user prompt :wink:

@Tom. Actually for my purposes, your original script worked perfectly. However, I will probably end up using your new one and NaOH's one for other projects, so thanks to you both!

1 Like

After some searching online, this MacDrifter post would have been where I found the macro and its AppleScript code:

http://www.macdrifter.com/2012/10/find-safari-tabs-by-substring.html

2 Likes