1

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
1

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 argument t, but then makes not use of the variable anywhere in the handler. The variable t that is utilised actually comes from the repeat with t in tabs of front window loop, where t is sequentially assigned the reference to each tab of the front window. Therefore, you can simply change the handler declaration to on 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 variable i to serve as a counter, then manually increment its value with each iteration. Instead, you can define i within the repeat construct itself, like this:

    repeat with i from 1 to the number of tabs in the front window
        set t to tab i of the front window
        if title of t contains the searchString then
            set the active tab index of the front window to i
            return
        end if
    end repeat

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 (or where).

For instance, instead of:

    repeat with win in win_List
        ...
        repeat with T in tab_list
            if searchString is in (title of T as string) then
                ...
            end if
            ...
        end repeat
    end repeat

you can use this:

    get every tab in every window whose title contains the searchString
1

Summary (TL;DR)

Store a reference to a tab or window

Either by id (which is fixed):

    set T to the id of the current tab in the front window
    ...
    set the URL of tab id T of the front window to "https://apple.stackexchange.com/"

Or by object (which is fixed):

    set T to the current tab in the front window
    ...
    set the URL of T to "https://apple.stackexchange.com/"

BUT not by index (which changes).


You have a couple of options when storing references to objects (such as tabs) in AppleScript. Specifically, you're asking about the id property of the tab. You have to be mindful here: there are two ways of referencing Chrome tabs and windows using numbers:

Referencing Tabs/Windows by id/index

❶・By index: e.g. tab 2 of window 1

This refers to the ordered position of the tab from leftmost to rightmost, i.e. tab 1 refers to the left tab, tab 2 refers to tab to the right of tab 1, etc..

These indices change if tabs move, open and close. For example, if you close the middle of three tabs, tab 3 then becomes tab 2.

With the windows collection, the index is the ordered position of the window from frontmost to rearmost, ordered by which window most recently received focus. window 1 is synonymous with the front window, and currently has focus. When you switch to a different Chrome window, this now becomes window 1, and the previous window 1 is now window 2.

Therefore, referencing an object and storing references to that object by its index property is a very unreliable method, with the exception of references to the front window or front tab where it only matters that the object currently has focus, regardless of which one it is. Therefore, tell the front tab of the front window to... is perfectly reasonable use of the index, and probably the only one.

Note, that the index property is a named property for window objects, which can be read and set (set it to 1 to bring it to the front). Although tab objects do get referred to by an index property, it is not a named property that one can read or set.

❷・By id: e.g. tab id 54 of window id 53

This is a unique, fixed property assigned to each object at the moment of its creation, and it typically ascends in value as each new object in a family of similar objects is created. So, generally speaking, window id 2088 was created after window id 25.

Importantly, the id provides a fixed reference value with which you can reliably and safely store objects in variables for later use, knowing that they will still refer to the exact same object regardless of where in your script the variable is evaluated and how the arrangement of objects has changed (until the object is destroyed).

Setting and Getting The Variable

When you wish to assign a reference to a tab or a window to a variable, you can choose to:

either store its actual id property value, then reference the tab by its id later on, the value of which will be the value of the variable;

or you can store the entire object itself in an AppleScript variable. This is often the easiest.

By id

To store by id, you can do so like this:

    set T to the id of the active tab of the front window

In Chrome, the active tab of the front window might return tab id 34 of window id 2045. The id of that tab object is 34, so T gets assigned the value 34.

Then, later, to perform actions on that same tab, which may no longer be the active tab, you can do so like this:

    tell tab id T of the front window to execute JavaScript...

(assuming the front window is still window id 2045; otherwise, you'll have to store the window id value in a separate value too.)

Note that the reference is to a tab id T and not tab T, which would be tab 34 (i.e. index 34, if you had 34 tabs open). The id label must be included.

By Object

To store the tab object itself, which is often preferable, you do so like this:

    set T to the front tab of the front window

Although I've implicitly used index values here, AppleScript actually references the tab and the window both by their id values (which it does so in almost all situations). So if you evaluate T, you'll find it contains tab id 34 of window id 2045 and not tab 1 of window 1.

You can also assign the tab object to a variable like this:

    set T to the first tab of window id 2045 whose URL is "chrome://newtab/"

and T will contain tab id 30 of window id 2045, which you can later call on to perform actions within/upon:

    set the URL of T to "https://stackexchange.com"

or

    tell T to close
  • that's perfect thank, I'm out of the office for a few day but can't wait to test all this. thank you. – Kevin Jun 29 '18 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .