I just posted with the subject “Getting Tab Index and Window ID in Safari”.
This script uses that technique to find a tab (or tabs) containing the given URL and brings the first hit to the front.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/20 17:30
# dMod: 2017/05/20 18:12
# Appl: Safari
# Task: Bring a tab containing a given URL to the front.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Bring, @Tab, @Containing, @URL, @Frontmost
------------------------------------------------------------------------------
tell application "Safari" to set tabWinList to tabs of windows whose URL contains theURL
try
tabWinList / 0
on error eMsg
set AppleScript's text item delimiters to {"Can’t make ", " into type real.", "of application \"Safari\"", " of window id", "«class bTab»"}
set tabWinList to text items of eMsg
set AppleScript's text item delimiters to linefeed
set tabWinList to tabWinList as text
set tabWinList to words of tabWinList
repeat with theWord in tabWinList
try
set contents of theWord to theWord as integer
on error
set contents of theWord to "null"
end try
end repeat
end try
if tabWinList ≠ {} then
set {tabIndex, winID} to items 1 thru 2 of (integers of tabWinList)
tell application "Safari"
tell window id winID
set index to 1
raiseWindowOne() of me
set current tab to tab tabIndex
end tell
end tell
end if
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on raiseWindowOne()
tell application "System Events"
tell application process "Safari"
tell window 1 to perform action "AXRaise"
end tell
end tell
end raiseWindowOne
------------------------------------------------------------------------------