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
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
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:
β’ 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
REQUIRED:
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)
http://forum.latenightsw.com/t/what-is-best-method-to-activate-google-chrome-tab-for-existing-url/600/16?u=jmichaeltx
~~~ MY QUESTION on the SD FORUM ~~~
What is Best Method to Activate Google Chrome Tab for Existing URL?
RESULTS:
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
activate
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"
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"
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.
@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.
@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
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.