SCRIPT: Activate Google Chrome Tab for Specified URL

Continuing the discussion from Bring a specified window of an app to the front (if there are more than one window):

@kreal, I decided to make a new topic for this.

This is an interesting topic/script. Last year I raised this question in the Script Debugger forum, and we had a little contest that was very enlightening. See

Also See:
Make Safari Window with URL FrontMost

UPDATED: 2018-10-13 18:00 GMT-5
VER: 1.2
Fix activation of Chrome to make frontmost window active.

property ptyScriptName : "Activate Google Chrome Tab for Specified URL"
property ptyScriptVer : "1.2" --  ADD Activate Google command
property ptyScriptDate : "2018-10-13"
property ptyScriptAuthor : "estockly" -- adapted for KM by JMichaelTX

  β€’ Activate Google Chrome Tab for Specified URL
      β€’ If URL is for existing Tab, that Window/Tab will be made frontmost
      β€’ Else a new Tab in frontmoat Window will be added for URL
RETURNS:  Chrome Window/TAB with Requested URL at Front

  1.  macOS 10.11.6+
  2.  Mac Applications
      β€’ Google Chrome
TAGS:  @CAT.Web @CAT.Windows @SW.Chrome @type.Example @Lang.AS @Auth.Other @CAT.URL

REF:  The following were used in some way in the writing of this script.

  1.  2017-05-14, estockly, Late Night Software Ltd.
      What is Best Method to Activate Google Chrome Tab for Existing URL? (Ques by @JMichaelTX)
~~~ MY QUESTION on the SD FORUM ~~~
What is Best Method to Activate Google Chrome Tab for Existing URL?


Congratulations Ed! Your script is the fastest by 2X when run against my standard test from above 
(10 win with 20 total tabs):
    @JMichaelTX:  0.39     @ccstone: 0.38       @estocky: 0.17


tell application "Keyboard Maestro Engine" to set urlToFind to getvariable "URLToFind"

tell application "Google Chrome"
  set windowTabList to URL of tabs of every window
  set targetURL to urlToFind
  set found to false
  set windowIndex to 1
  repeat with thisWindowsTabs in windowTabList
    set TabIndex to 1
    repeat with TabURL in thisWindowsTabs
      if TabURL as text = targetURL then
        set index of window windowIndex to 1
        set active tab index of window 1 to TabIndex
        set found to true
        exit repeat
      end if
      set TabIndex to TabIndex + 1
    end repeat
    if found then exit repeat
    set windowIndex to windowIndex + 1
  end repeat
  if not found then
    make new tab at window 1 with properties {URL:targetURL}
  end if
end tell

--- Force macOS to Actually Make the Window FrontMost ---

tell application "System Events"
  tell application process "Google Chrome"
    tell (front window) to perform action "AXRaise"
  end tell
end tell

return "OK"


The script works very well!

1 Like

I'm embarrassed to ask you this: where do I paste the URL in question in your apple script?
thanks very much

1 Like

Nothing to be embarrassed about. It is my bad for not making it clear.
Normally I post a full macro with any script I post, but I deviated in this case.

Just set the KM Variable "URLToFind" to your target URL, and then run the script.
The key is the first line of code:
tell application "Keyboard Maestro Engine" to set urlToFind to getvariable "URLToFind"



thanks very much !

To make this work with just tab titles I had to change a small part of the script to this:

set windowTabList to title of tabs of every window

That way I was able to search by tab title. Ideally I would want to use a more flexible approach for partial match or just containing text...

this macro is a gem !

just a minor issue. I discovered that for the macro to work, you can't have Chrome active without any tab already open.

Can I simply just activate the 1st tab (irrespective of url)?

I am doing something with only 2 tabs anyway

Yes, easily:


1 Like

You don't need KM for that. ⌘1 thru ⌘9 will activate the nth Tab in that window.
Also, βŒƒβ‡₯ and βŒƒβ‡§β‡₯ will move one tab forward and backward.

1 Like

Thanks a lot @gglick and @JMichaelTX

Can you provide the script with your newly added modification ? I'm not familiar with this.

@JMichaelTX, this script is indeed a gem! Got it to work easily.

I use it to activate the different Google apps: chat, hangouts, drive, calendar, slides, etc.
Now, a lot of this URL, depending on where you are in the app, have a trail after the base URL.

For instance, calendar could be, but also
Chat has a URL trail depicting the chat you're in.

So I would like to adapt the URL "mapping" limited to the length of the URL I search for.

In the calendar example, if I search for, whatever comes after this string should be ignored.

I'm not familiar with Apple Script, so therefor my question if you could add this logic?

Something like:

TabURL = Left ( TabURL, len ( targetURL ) )

Thanks in advance!

@JMichaelTX Google was my friend (couldn't wait and sitting sick at home ;-).

It was a simple as adding this line within the repeat.

		set lenTabURL to the length of TabURL
		set lentargetURL to the length of targetURL
		if lenTabURL > lentargetURL then
			set TabURL to text 1 thru lentargetURL of TabURL
		end if


1 Like

Hey there. I'm having the same challenge you did with wanting to open my web app, even if there are trailing characters in the url after the static part. I tried inserting your code into my script but couldn't find the correct place. Would you mind sharing entire script so I can see context of this code snippet? Thank you.

@JMichaelTX thanks again for an excellent macro. Just curious: does it work for you with google calendar opening in chrome ? The only case where I can't get it to work. Always opens in another tab.

Sorry @ronald, but I don't use Google Calendar so I have no idea.

1 Like


Sorry @jessestarrphoto, just seeing your question now, I should switch on email warning...
Here my script.