1

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
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 ¬
  • Thank you that's great, I just wondering, if I want to do JavaScript inside all the matching tab, would I just change "first Item" and add a repeat statement here: try set tabIndex to (get index of first item of tabList) end try – Kevin Oct 12 '20 at 16:13
  • @Kevin, In your OP, as coded, you where not acting on multiple tabs containing the same search string if more than one was found, only acting on the first tab returned. The handler I wrote does the same thing. That said, any code can be modified to do something differently, albeit it depends on what one is trying to do as to how easily the code can be modified. I'd suggest you try modifying it to suite a different need of yours and if you get stuck somewhere, post a different question. Thanks. – user3439894 Oct 12 '20 at 16:32
  • @Kevin, In other words I believe this answer covers what you were doing and anything different should be a different question so as not to muddy up what's already been presented. This site works best with one question per question. I'll be more than glad to help where I can if you end up posting another question to cover something explicitly different then what was asked in your OP here. – user3439894 Oct 12 '20 at 16:37
  • Yes you right sorry, that is perfect anyway thanks again – Kevin Oct 12 '20 at 17:01
  • I wrote a handler a while ago to filter Safari tabs by URL. While there's nothing I can add to this (excellent) answer, it prompted me to upload mine as a gist, as I keep meaning to do a big GitHub update but haven't the time. The intention behind it was to eliminate any repeat loops and thus––in AppleScript terms––optimise the speed and efficiency, whilst still being able to operate over all windows and all tabs. In regular use for a while but let me know if it blips. – CJK Oct 13 '20 at 15:41

You must log in to answer this question.

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