Displaying list of all open tabs in chrome / safari

I found this very useful AppleScript to list all open tabs (works across multiple windows) for Chrome / Safari. Once you click on it, it will also automatically switch to that specific tab. (This is something I really really wanted). Technically, we’d have to use two scripts for the same, adding them below.

Chrome:

Chrome Tab Switcher.kmmacros (5.8 KB)

List all tabs

tell application "Google Chrome"
	set tabList to {}
	-- Get all windows
	set windowList to every window
	repeat with aWindow in windowList
		set winID to id of aWindow
		-- Get all tabs in this window
		set currentTabs to every tab of aWindow
		set tabIndex to 0
		repeat with aTab in currentTabs
			set tabIndex to tabIndex + 1
			set tabName to title of aTab
			-- Format: "WindowID,TabIndex__TabName" (Double underscore separator)
			copy (winID as string) & "," & (tabIndex as string) & "__" & tabName to end of tabList
		end repeat
	end repeat
end tell

-- Return the list as text separated by new lines
set AppleScript's text item delimiters to "
"
return tabList as string

Save to a variable, in this example local_ChromeTabsRaw.

Prompt with list and show all available tabs.

Switch to the selected tab:

tell application "Keyboard Maestro Engine"
	set selectedTab to getvariable "ChromeTabDest"
end tell

-- Check if selection is valid
if selectedTab is not "" then
	-- Clean up the string if it contains extra text (just in case)
	set AppleScript's text item delimiters to "_"
	set cleanCoords to text item 1 of selectedTab
	
	-- Parse "WindowID,TabIndex"
	set AppleScript's text item delimiters to ","
	if (count of text items of cleanCoords) is greater than or equal to 2 then
		set winID to text item 1 of cleanCoords as integer
		set tabIdx to text item 2 of cleanCoords as integer
		
		tell application "Google Chrome"
			activate
			try
				-- 1. Bring the specific window to the front
				set index of window id winID to 1
				
				-- 2. Switch to the specific tab index
				set active tab index of window id winID to tabIdx
			on error
				beep
			end try
		end tell
	end if
end if
Chrome Macro Image

This text will be hidden

Safari:

Safari Tab Switcher.kmmacros (6.1 KB)

List all tabs:

tell application "Safari"
	set tabList to {}
	-- Iterate through every window
	set windowList to every window where visible is true
	repeat with aWindow in windowList
		set winID to id of aWindow
		-- Iterate through every tab in the window
		set currentTabs to every tab of aWindow
		repeat with aTab in currentTabs
			set tabName to name of aTab
			set tabIndex to index of aTab
			-- Format: "WindowID,TabIndex__TabName" (Double underscore is a separator)
			copy (winID as string) & "," & (tabIndex as string) & "__" & tabName to end of tabList
		end repeat
	end repeat
end tell

-- Return the list as text separated by new lines
set AppleScript's text item delimiters to "
"
return tabList as string

Save to a variable, in this example local_TabsRaw.

Prompt with list and show all available tabs.

Switch to the selected tab:

tell application "Keyboard Maestro Engine"
	-- Get the variable from KM
	set selectedTab to getvariable "SafariTabDest"
end tell

-- Clean up the string if it contains extra text (e.g., "99678,1_ Name")
set AppleScript's text item delimiters to "_"
set cleanCoords to text item 1 of selectedTab -- This keeps just "99678,1"

-- Now split the clean coordinates into Window ID and Tab Index
set AppleScript's text item delimiters to ","
if (count of text items of cleanCoords) is greater than or equal to 2 then
	set winID to text item 1 of cleanCoords as integer
	set tabIdx to text item 2 of cleanCoords as integer
	
	tell application "Safari"
		activate
		try
			-- 1. Bring the specific window to the front
			set index of window id winID to 1
			
			-- 2. Switch to the specific tab
			set current tab of window id winID to tab tabIdx of window id winID
		on error
			beep -- Beep if the tab no longer exists
		end try
	end tell
else
	-- Troubleshooting: This runs if the format isn't "Number,Number"
	display notification "Error: Could not read Tab ID" with title "Safari Switcher"
end if
Safari Macro Image

Hope it’s useful!

I think a couple of your AS actions don't do anything. Even if they did, it's much better to put as much of your AS as you can into a single action, so you don't have to pay the "instantiation cost" three times.

FWIW, this is the macro I use. It's shorter, but I suspect that's more my lack of error checking than anything!

Activate Safari Tab From Prompt.kmmacros (3.9 KB)

That makes sense, I’ll look into shortening my AS actions. I’m still pretty new to this, thanks for pointing it out!

It's the other way round -- have fewer but longer!

With each KM "Execute an AppleScript" Action there's overhead for creating (instantiating) an environment for the script to run in. That environment, along with any values you don't pass back to the KM Engine via setvariable or an explicit return, is destroyed when the script has completed -- it doesn't get re-used if you call another AS with a later Action.

How long the instantiation takes mainly depends on your CPU speed, but it's commonly 30-80 milliseconds -- most KM Actions only take a millisecond or two. Not much of a slowdown for one Action, but what you really want to avoid is calling AS multiple times, such as in a loop.

Silly demo time!

AS Instantiation TIme Demo.kmmacros (7.1 KB)

Image

Which, on my machine and adding up from 0 to 100, takes 20 almost seconds longer to execute the "loopy" section than the single script section:

And as an even faster alternative, the shell usually wins out, and does in this case by a factor of 15:

That's just a one-liner shell loop:

j="$KMVAR_Local_theNum1"; while [ $j -le 99 ]; do ((j++)); done; echo $j

I know that wasn't the point of your demonstration, but just an added item to look into if you're writing a macro that needs to do stuff over and over and over again: If you can do something in the shell or in AppleScript, the shell will be notably quicker in Keyboard Maestro.

-rob.

FWIW a variant which

  1. Sorts the menu by (case-insensitive) name of tab
  2. Batch fetches tab names and indices from each open window

Activate Safari Tab From Prompt (Sorted menu).kmmacros (7.7 KB)

1 Like