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