xcode ios simulator: how to set focus on specific window

591 Views Asked by At

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

  • Switching focus using "open": open -a Simulator --args -CurrentDeviceUDID <id>. But it opens the last focused simulator instance instead of the instance with passed id.
  • Switching focus using simctl tool. I tried to open URL in the specific instance of the simulator xcrun simctl openurl <id> "https://www.google.com", but it doesn't change the focus.
  • using applescript - it still focusing on the last focused simulator instance:
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

1

There are 1 best solutions below

6
On

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.