Chris, thanks so much for pointing out this important design pattern (use lists instead of objects).
My revised script now runs in 0.28 seconds for 10 windows with a total of 20 tabs.
####Here are the key changes:
set winIDList to id of every window
-------------------------------------
repeat with aWinID in winIDList -- search through every window ID
-----------------------------------
set tabIndex to 0
set oWin to window id aWinID
set urlList to URL of tabs of oWin
-------------------------------------
repeat with aTabURL in urlList -- search through every tab URL
------------------------------------
set tabIndex to tabIndex + 1
set urlTab to aTabURL as text
. . .
###Script Revised to Use Lists instead of Objects
(*
PURPOSE: Show/Create Tab with Given URL
* If Tab with URL exists, just Activate it
* If not, create new Tab with URL
*)
property LF : linefeed
property defaultBrowser : "Google Chrome"
set scriptResults to "TBD"
######### JUST FOR TESTING ###########
set urlMatch to "EXACT"
set urlToFind to "https://forum.keyboardmaestro.com/"
--------------------------------------
tell application "Google Chrome"
------------------------------------
-- activate
set urlFound to false
--- SEARCH THROUGH EVERY TAB OF EVERY WINDOW UNTIL A MATCH IS FOUND ---
-- (If no match, open new Tab)
set winIDList to id of every window
set numWin to length of winIDList
-------------------------------------
repeat with aWinID in winIDList -- search through every window
-----------------------------------
set tabIndex to 0
log aWinID
### ? Surely there must be a better way to get the tab index ###
-- apparently the only way to get tab index is to count
set oWin to window id aWinID -- (first window whose id is aWinID)
set urlList to URL of tabs of oWin
-------------------------------------
repeat with aTabURL in urlList -- search through every tab
------------------------------------
set tabIndex to tabIndex + 1
set urlTab to aTabURL as text
if (urlMatch = "CONTAINS") then -- also handles Domain Name match
if (urlTab contains urlName) then
set urlFound to true
end if
else if (urlMatch = "EXACT") then
if (urlTab = urlToFind) then
set urlFound to true
end if
else if (urlMatch = "ENDSWITH") then
if ((urlTab ends with urlToFind) or (urlTab ends with (urlToFind & "/"))) then
set urlFound to true
end if
end if
--------------------
if urlFound then
------------------
set active tab index of oWin to tabIndex
set index of oWin to 1
set scriptResults to "OK" & LF & ¬
"Existing Tab with URL was Activated." & LF & "Match Type: " & urlMatch & LF & urlToFind
-----------------
exit repeat
-----------------
end if
end repeat
------------------------------
if urlFound then exit repeat
------------------------------
end repeat
if (urlFound is false) then
if (urlToFind does not start with "http") then set urlToFind to "http://" & urlToFind
--- OPEN URL IN NEW TAB ---
tell front window
set oTab to make new tab at end of tabs with properties {URL:urlToFind}
end tell
set scriptResults to "OK" & LF & "NEW Tab with URL was Opened." & LF & "Match Type: " & urlMatch & LF & urlToFind
end if -- urlFound is false
end tell
return scriptResults
--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~