Activate First Matching Tab Subroutine

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:

  1. A string to search for
  2. What to search, either tab name or url
  3. One of starts, ends, or contains for match position
  4. A choice of (AppleScript supporting) browser -- Safari or Chrome 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)

Image

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.

Image

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)

Image

Open the KM Forum in Google Chrome, matching by part of the URL:
KMForum.kmmacros (3.9 KB)

Image

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 :wink:

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.

4 Likes

@Nige_S woof, what a thing of beauty! - thanx man

Looks nice, @Nige_S; thanks for sharing. Here’s a macro that is somewhat related…

<sigh>

I should have known I was reinventing the wheel -- and a square one at that! Thanks, @_jims.

In my defence -- the one thing I didn't want to have happen was the browser activating and bringing all its windows to the front. I'll nearly always be working in a different app and will want to bring just the window of interest forward, often positioning beside the original app's front window or on another display.

I should have realised I could manage that by simply commenting out the activate statements in your macro...

1 Like

I do that often. I've even done it for one of my own macros. :rofl:

In all seriousness @Nige_S, I think many users (like me) learn best with examples; so the more, the merrier.

Unlike yours, the macro I shared is not a subroutine. I don't remember for sure, but I think I did that because I was wanting to make it available to those that hadn't upgraded Keyboard Maestro. With recent macros I've shared, I've not made that provision. But now, several months later, I should probably convert it to a sub.

1 Like