The following example AppleScript code is one method that works for me:
Example AppleScript code:
tell application "Simulator"
activate
set winName to the name of ¬
the first window ¬
whose visible is true ¬
and index is not 1
end tell
tell application "System Events" to ¬
perform action "AXRaise" of ¬
window winName of ¬
process "Simulator"
Notes:
The example AppleScript code assumes you have two Simulator windows visible, and it will toggle raising between the two. Obviously this is just an example and you'll need to modify and incorporate the methodology into you own code.
In your example AppleScript code, window n refers to its index as in:
index (integer) : The index of the window, ordered front to back.
The quoted line above is from the AppleScript dictionary for Simulator in Script Editor.
The z-order of index starts at: 1
Using System Events and perform action "AXRaise" of window ...
can be done by it's name
property or it's index
property.
The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.