Bring Safari Window to Front

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?

Thanks

In you example, you’re trying to bring to front the first window…

If you’re on the latest version of Catalina (Safari 14.1.1), I can confirm that the index property is still working:

tell application "Safari"
	activate
	set index of window -1 to 1
end tell

And if you need to activate a tab within a window:

tell application "Safari"
	activate
	set current tab of window 1 to tab -1 of window 1
end tell

Another suggestion:

tell application "System Events" to tell process "Safari"
	perform action "AXRaise" of the last window
end tell

I forgot to say the OS is 11.4 (Big Sur); Safari 14.1.1 .

A refernce to a Safari window eg.,

tell application "Safari"
	set aWindowID to front window
end tell

has to stay in the Safari tell box it seems.

A solution seems to be to close and reopen the window.

tell application "Safari"
	set aWindowID's visible to false
	set aWindowID's visible to true
end tell

This seems to kinda work.


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.

(BAD Apple!)

-Chris

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

If so, that’d be just the ticket.