I'm using this script which is finding the tab I want, then bring the tab to focus.
set searchString to "Tab I'm Looking FOR"
tell application "Google Chrome"
set win_List to every window
set win_MatchList to {}
set tab_MatchList to {}
set tab_NameMatchList to {}
repeat with win in win_List
set tab_list to every tab of win
repeat with t in tab_list
if searchString is in (title of t as string) then
set end of win_MatchList to win
set end of tab_MatchList to t
set end of tab_NameMatchList to (id of win as string) & ". " & (title of t as string)
end if
end repeat
end repeat
if (count of tab_MatchList) is equal to 1 then
set w to item 1 of win_MatchList
set index of w to 1
my setActiveTabIndex(t, searchString)
else if (count of tab_MatchList) is equal to 0 then
display dialog "No match was found!" buttons {"OK"} default button 1
else
set which_Tab to choose from list of tab_NameMatchList with prompt "The following Tabs matched, please select one:"
set AppleScript's text item delimiters to "."
if which_Tab is not equal to false then
set tmp to text items of (which_Tab as string)
set w to (item 1 of tmp) as integer
set index of window id w to 1
my setActiveTabIndex(t, searchString)
end if
end if
end tell
on setActiveTabIndex(t, searchString)
tell application "Google Chrome"
set i to 0
repeat with t in tabs of front window
set i to i + 1
if title of t contains searchString then
set active tab index of front window to i
return
end if
end repeat
end tell
end setActiveTabIndex
This work fine, but how can I save the tab number as a variable so if I need to do more stuff in this actual tab I can just do something like this (imagining the tab number is saved as "TABNUMBER")
tell application "Google Chrome"
set myTab to front window's tab TABNUMBER
set URL of myTab to "https:..."
end tell
Best Answer
Notes on the script
I'm submitting this second "answer" to address some specifics about the code sample you provided in your post. As this doesn't answer your question directly (which my first answer does), and because my first answer is already very long; I felt it sensible to compose this separately.
To start, your
on setActiveTabIndex(t, searchString)
handler definition specifies an argumentt
, but then makes not use of the variable anywhere in the handler. The variablet
that is utilised actually comes from therepeat with t in tabs of front window
loop, wheret
is sequentially assigned the reference to eachtab
of thefront window
. Therefore, you can simply change the handler declaration toon setActiveTabIndex(searchString)
, and then adjust the rest of the script accordingly wherever the handler is called, i.e. by removing the first argument.One oddity in the way you implemented the
repeat
loop in this handler is how you declared a new variablei
to serve as a counter, then manually increment its value with each iteration. Instead, you can definei
within therepeat
construct itself, like this:The rest of the script is okay, but it relies on a lot of manual iterative loops that are not only difficult to read within the script—making it hard to discern what's happening at each stage—but it is also less efficient in execution.
Instead, you could utilise AppleScript's object collections that can be filtered using
whose
(orwhere
).For instance, instead of:
you can use this: