Rostislav Shtanko
Rostislav Shtanko

Reputation: 724

xcode ios simulator: how to set focus on specific window

I have n instances of xcode ios simulator and I want to switch a focus on instance with specified id. I tried next things:

tell application "System Events" to tell process "Simulator"
    perform action "AXRaise" of window 0
end tell

Do you have any idea, how to do this? enter image description here

Upvotes: 0

Views: 742

Answers (1)

user3439894
user3439894

Reputation: 7555

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, windown 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.

Upvotes: 1

Related Questions