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
Best Answer
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 useset theTabNumber to index of item 1 of t
instead ofset 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:
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:
Alternate Methods
If you have commented out the
if winID is not missing value then
block in the handler in order to execute thedo JavaScript ...
command in the background, change:To:
Or use an even different approach: