In case it's useful to anyone -- I'm sure it can be improved, so feel free to run with it!
SUB - Activate First Matching Tab
A subroutine that takes 4 parameters:
- A string to search for
- What to search, either tab
name
orurl
- One of
starts
,ends
, orcontains
for match position - A choice of (AppleScript supporting) browser --
Safari
orChrome
as written
...and returns a result of 0
on success or 1
if no match is found.
The script walks through the chosen browser's open windows in index order, looking for a matching tab. The first match found will be promoted to the current/active tab for that window, and that window brought to the top of the browser's window stack.
Neither the window nor the browser itself is activated -- any change of frontmost window/application is left to the calling macro.
SUB - Activate First Matching Tab.kmmacros (5.4 KB)
Use Case
For when you want to go to a web page by macro and, if the page is already open, have that pop to the front instead of opening a window with a new instance.
You could give each caller its own hot key trigger or create a palette to fire them from. I'm putting them all in a Group called "Bookmarks" and using "trigger Macro by Name" to open them.
So to open the KM Forum using the caller below it would be ⌃⌥B to get the prompt dialog and then kmf
and the Return key.
Example Callers
Open the KM single page manual in Safari, matching by tab name:
KMManual.kmmacros (4.0 KB)
Open the KM Forum in Google Chrome, matching by part of the URL:
KMForum.kmmacros (3.9 KB)
What, no RegEx?
The AppleScript whose
clause is nice and fast and, while matching is limited, I've been able to target all my usual tabs with carefully chosen simple string matches. Anyone using this will have KM (obviously!) and so could make use of KM's regex engine in the AS -- consider that an "exercise for the reader", at least for now
Note
I nearly always use Safari, only slipping to Chrome when a site demands it, which is why this is a single subroutine covering both browsers -- I've added it to Favorites with Safari pre-filled. But it would be easy enough to split it into one sub for each browser should you prefer fewer parameters and an extra sub.