set mt to 9
set theApp to "Safari"
tell application "Safari"
# Get the full name of the wanted window
set wanted to name of window 2
end tell
tell application "System Events" to tell process theApp
set frontmost to true
tell menu bar 1 to tell menu bar item mt to tell menu 1
# Build a list of items of menu Windows
set menuItems to name of every menu item
end tell
end tell
set windowNames to {}
repeat with aName in reverse of menuItems
if class of aName is not text then exit repeat
# Build a list of menu items pointing a window
set end of windowNames to (aName as text)
end repeat
-- windowNames
repeat with theWindow in windowNames
if theWindow contains "…" then
set maybe to my decoupe(theWindow, "…")
if wanted starts with maybe's item 1 and wanted ends with maybe's item -1 then
set theWindow to maybe's item 1 & "…" & maybe's item -1
exit repeat
else
# The menu item doesn't contain "…" so compare to window name
if theWindow = wanted then exit repeat
end if
end if
end repeat
tell application "System Events" to tell process theApp
set frontmost to true
# Bring the selected window to front
tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item (theWindow as text)
end tell
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) samedi 4 juin 2016 21:30:54