AppleScript: set Safari tab to specific index

I have an AppleScript which search for tab and run a JS in the selected tab, It's work because of this :

set current tab of w to t
        set index of w to 1

but I would like to improve the code so I can refer the tab, at the moment as you can see I'm only referring the window ID and it's ok when I know the tab number but how can I also refer the tab?

do JavaScript "document.getElementsByClassName('myJShere')[0].click();" in tab tab 1 of window id theWindowID

I tried set theTabNumber to (id of t as string) but that doesn't work

t in my current example, return item 3 of {tab 1 of window id 6615 of application "Safari", tab 2 of window id 6615 of application "Safari", tab 3 of window id 6615 of application "Safari", tab 4 of window id 6615 of application "Safari"}

I tried :

set theTabNumber to item 1 of t
return theTabNumber

but this return

tab 3 of window id 6615 of application "Safari"

I suppose I could use text delimiting but that doesn't seems right

full script

set theTABID to 0
set searchpat to "Facebook"

tell application "Safari"
    set winlist to every window
    set winmatchlist to {}
    set tabmatchlist to {}
    set tabnamematchlist to {}
    repeat with win in winlist
        set ok to true
        try
            set tablist to every tab of win
        on error errmsg
            --display dialog name of win as string
            set ok to false
        end try
        if ok then
            repeat with t in tablist
                if searchpat is in (name of t as string) then
                    set end of winmatchlist to win
                    set end of tabmatchlist to t
                    set end of tabnamematchlist to (id of win as string) & "." & 1 & ".  " & (name of t as string)
                    set theTABID to (id of win as string)
                    ##return (index of t as string)
                    --display dialog name of t as string
                else if searchpat is in (URL of t as string) then
                    set end of winmatchlist to win
                    set end of tabmatchlist to t
                    set end of tabnamematchlist to (id of win as string) & "." & 1 & ".  " & (URL of t as string)
                    --display dialog name of t as string
                end if
            end repeat
        end if
    end repeat
    
    set w to item 1 of winmatchlist
    set t to item 1 of tabmatchlist
    
    set current tab of w to t
    set index of w to 1
    
end tell
 

if theTABID = 0 then
    
    
    set searchpat to "Twitter"
    ##set searchpat to "Case Management"
    tell application "Safari"
        set winlist to every window
        set winmatchlist to {}
        set tabmatchlist to {}
        set tabnamematchlist to {}
        repeat with win in winlist
            set ok to true
            try
                set tablist to every tab of win
            on error errmsg
                --display dialog name of win as string
                set ok to false
            end try
            if ok then
                repeat with t in tablist
                    if searchpat is in (name of t as string) then
                        set end of winmatchlist to win
                        set end of tabmatchlist to t
                        set end of tabnamematchlist to (id of win as string) & "." & 1 & ".  " & (name of t as string)
                        set theTABID to (id of win as string)
                        ##return (index of t as string)
                        --display dialog name of t as string
                    else if searchpat is in (URL of t as string) then
                        set end of winmatchlist to win
                        set end of tabmatchlist to t
                        set end of tabnamematchlist to (id of win as string) & "." & 1 & ".  " & (URL of t as string)
                        --display dialog name of t as string
                    end if
                end repeat
            end if
        end repeat
        
        set w to item 1 of winmatchlist
        set t to item 1 of tabmatchlist
        
        set current tab of w to t
        set index of w to 1
        
    end tell
end if

 
tell application "Safari"
    do JavaScript "document.getElementsByClassName('a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7 ltmttdrg g0qnabr5 ')[0].click();" in current tab of window id theTABID
end tell

Solution 1:

If you are just trying to set the value of the tab index of item 1 of t to a variable, then is there some reason you can not just use set theTabNumber to index of item 1 of t instead of set theTabNumber to item 1 of t? -- This code, set theTabNumber to index of item 1 of t returns an integer, representing the r/o index of the tab, ordered left to right in that window.

That said, looking at the code in your OP, as coded, the do JavaScript ... command is going to execute regardless of the result of both search strings, which of course doesn't make any sense!

If you are testing for multiple search strings, it is more logical to execute different do JavaScript ... commands based on the results.

That said, as an exercise (hopefully not in futility) I decided to write a handler from scratch that does the following:

  • Takes a search string.
  • Searches the name of every tab of every window for the search string.
  • If the search string is not found in the name, it searches the URL for it.
  • If the search string is found, it brings the target tab and window to the front and returns a list with the tab index and window id.
  • If the search string is not found, it returns a list with missing value for each item of the list, which can be used as the flag to execute a different search string.

For testing purposes, with multiple windows having multiple tabs with at least one set to "https://www.facebook.com" or "https://twitter.com" on different test runs of the example AppleScript code, and not logged into either, will search for "Facebook" or "Twitter" to bring their windows to the front and be the current tab to execute a do JavaScript ... command that will click the Create New Account button on Facebook or the Sign up button on Twitter.

Note: The code that brings the target window to the front and sets the target tab to the current tab can be commented out and use what's returned from the handler to target the tab by its index and the window by its id so it can be done in the background if wanted.

Example AppleScript code:

findTabContainingSearchString("Facebook")
if result does not contain missing value then
    tell application "Safari" to tell document 1 to do JavaScript ¬
        "document.getElementsByClassName('_42ft _4jy0 _6lti _4jy6 _4jy2 selected _51sy')[0].click();"
else
    findTabContainingSearchString("Twitter")
    if result does not contain missing value then
        tell application "Safari" to tell document 1 to do JavaScript ¬
            "document.getElementsByClassName('css-901oao r-1awozwy r-jwli3a r-6koalj r-18u37iz r-16y2uox r-1qd0xha r-a023e6 r-vw2c0b r-1777fci r-eljoum r-dnmrzs r-bcqeeo r-q4m81j r-qvutc0')[0].click();"
    end if
end if


on findTabContainingSearchString(searchString)
    tell application "Safari"
        set tabIndex to missing value
        set winID to missing value
        set winList to windows
        repeat with w in winList
            set tabList to (tabs of w whose name contains searchString)
            try
                set tabIndex to (get index of first item of tabList)
            end try
            if tabIndex is not missing value then
                set winID to id of w
                exit repeat
            else
                set tabList to (tabs of w whose URL contains searchString)
                try
                    set tabIndex to (get index of first item of tabList)
                end try
                if tabIndex is not missing value then
                    set winID to id of w
                    exit repeat
                end if
            end if
        end repeat
        if winID is not missing value then
            set current tab of window id winID to tab tabIndex of window id winID
            set index of window id winID to 1
        end if
        return tabIndex & winID
    end tell
end findTabContainingSearchString

Alternate Methods

If you have commented out the if winID is not missing value then block in the handler in order to execute the do JavaScript ... command in the background, change:

tell application "Safari" to tell document 1 to do JavaScript ¬

To:

set {tabIndex, winID} to result
tell application "Safari" to tell tab tabIndex of window id winID to do JavaScript ¬

Or use an even different approach:

set {tabIndex, winID} to findTabContainingSearchString("Facebook")
if {tabIndex, winID} does not contain missing value then
tell application "Safari" to tell tab tabIndex of window id winID to do JavaScript ¬