AppleScript : do JS on different Safari windows
It's not the best (won't work if the same tab is open in different windows) but This seems to work 

set searchpat to "My Tab"
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
            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) &amp; "." &amp; 1 &amp; ".  " &amp; (name of t as string)
                    set theWindowID to (id of win as string)
                    ##return (index 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


then I can refer the window using theWindowID 

e.g:

tell application "Safari" to tell window id theWindowID to set current tab to tab 1


or 

do JavaScript ResolutionPopUp in tab 1 of window id theWindowID


(I'm referring the tab manually)