In the past I did something like this.
First I obtained a Safari window id:
tell application "Safari"
set aWindowID to front window
end tell
Then when I wanted to bring it to the front:
tell application "Safari"
set the index of aWindowID to 1
end tell
I ran a script which ‘worked’ before and it doesn’t work now. (Safari’s dictionary is telling me the index property is write only. Not correct. I was looking at Tab’s index property.)
Is this a new change?
In any case is there a scriptable way to bring Safari windows to the front?
tell application "Safari"
activate
repeat with i from 1 to 100
set index of window -1 to 1
set w1 to front window
delay 1
end repeat
end tell
Watching the event log with Script Debugger in the background w1 is showing a rotation of the windows and the windows in Safari are coming to the front EXCEPT that asside from the intial window which was frontmost the close, minimize and full screen buttons seem disable (not colored) AND if you CMD-TAB back to Safari after the script completes the original frontmost window returns to the front when it wasn’t at the end of the script.
This is such a long standing bug in macOS. I think it goes back to Mavericks but can’t remember for certain now.
It’s just unbelievable that Apple can’t be bothered to fix something so obvious for so long, but that’s rather typical of them I’m afraid.
This works for me on Mojave.
--------------------------------------------------------
tell application "Safari"
set winName to name of window 2
raiseWindowByName(winName) of me
end tell
--------------------------------------------------------
on raiseWindowByName(winName)
tell application "System Events"
tell (first application process whose frontmost is true)
tell window winName
perform action "AXRaise"
end tell
end tell
end tell
end raiseWindowByName
--------------------------------------------------------
Unfortunately at present I can’t test with Big Sur, because the remote system I can access to won’t allow the automation permission setting for Safari to stick. The dialog flashes by which prevents me from interacting with it, and the System Preference checkbox won’t stay checked.
I remember exploring this issue in Catalina, and the problem seemed to stem from AppleScript returning a reference to window class objects using the index key form rather than the id. But I recall (possibly) a problem retrieving the id property of a window, like perhaps it didn’t exist.
In Big Sur, it would be worth checking whether the following line executes successfully, and that it returns something meaningful:
tell app "Safari" to return the id of the front window