AppleScript to find a tab by its name in Google Chrome I'v forgotten how to find a tab by its name and return the value (e.g tab 2) and eventually set the tab as the active tab too.

I've tried the following, but it's not working:

set titleString to "
"

tell application "Google Chrome"
    set window_list to every window # get the windows

    repeat with the_window in window_list # for every window
        set tab_list to every tab in the_window # get the tabs

        repeat with the_tab in tab_list # for every tab
            set the_title to the title of the_tab # grab the title
            if the_tab contains (Name to search" as text) then
                display notification "the_tab"
            end if
             return # concatenate
        end repeat
    end repeat
end tell


I also tried to start something with JavaScript: 

tell application "Google Chrome"
    set window_list to every window
    repeat with the_window in window_list
        set tab_list to every tab in the_window
        tell tab_list to set TheTab to execute javascript "document.title"
    end repeat
end tell


But then I get:


  {«class CrTb» id 4 of window id 1 of application "Google Chrome",
  «class CrTb» id 9 of window id 1 of application "Google Chrome",
  «class CrTb» id 2 of window id 1 of application "Google Chrome",
  «class CrTb» id 189 of window id 1 of application "Google Chrome"}
  doesn’t understand the “execute” message.


How can I proceed?
Not being sure of the total scope of what you're trying to accomplish, the follow code may be more then you needed. However, it does allow for searching the Tab's Name and then sets the Tab containing the search string as the active tab.

The code below is a rework of the code presented in Find Safari Tabs with AppleScript. It would have been nice to have been able to change tell application "Safari" to tell application "Google Chrome" and the script worked, but because of the differences in the properties of a Tab between the two Apps, this is why that wouldn't work.

What this script does:


Displays a dialog box in which you type what to search for within the Names of the Tabs.
If the search string is matched in only one Tab, then that Tab is set to the active tab index, meaning that Tab is now the current tab. If there's more then one window, the window containing that Tab is brought to the front of all other Google Chrome windows.
If the search string makes multiple matches, then a list dialog is presented to choose from, then that Tab is set to the active tab index, meaning that Tab is now the current tab. If there's more then one window, the window containing that Tab is brought to the front of all other Google Chrome windows.
If the search string is not matched, a dialog box stating "No match was found!" is displayed.




set searchString to text returned of (display dialog "Enter a string to search for:" default answer "" with title "Find Google Chrome Tab")

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) &amp; ".  " &amp; (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:"
        if which_Tab is not equal to false then
            set oldDelims to (get AppleScript's text item delimiters)
            set AppleScript's text item delimiters to "."
            set tmp to text items of (which_Tab as string)
            set w to (item 1 of tmp) as integer
            set AppleScript's text item delimiters to oldDelims
            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