0

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

Rostislav Shtanko
  • 704
  • 2
  • 9
  • 30
  • If you want to use **System Events** and `perform action "AXRaise" of window ...` it needs to be the `name` of the _window_, not a _number_ as you are currently trying to do it. – user3439894 Aug 23 '21 at 12:52
  • how can I get a name of the window? Trying to do this: `set wList to every window whose visible is true`, and getting this `(*window id 54456, window id 54541, window id -1*)` – Rostislav Shtanko Aug 23 '21 at 13:00
  • I've update my answer with some additional information. Please take a look and let me know if you have any questions about implementing its methodology into you own _code_. – user3439894 Aug 23 '21 at 14:30

1 Answers1

1

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.

user3439894
  • 7,266
  • 3
  • 17
  • 28
  • thanks, your example works as `Cmd + Tab`, but I need more complicated thing - I need to switch on window with a specified id. The problem is that windows id's order is changing each time when I'm switching to another window according to z-index. – Rostislav Shtanko Aug 23 '21 at 15:49
  • RE: "thanks, your example works as Cmd + Tab, but I need more complicated thing" -- I understand that and it was merely an _example_ and not meant to be implemented literally. -- RE: "The problem is that windows id's order is changing each time when I'm switching to another window according to z-index" -- The `id` _property_ and the `index` _property_ of a _window_ are two different things, and it's the latter that's relevant to the _z-order_ not the former. If you could clarify explicitly and specifically what your needs are, I'm sure I can give you better _example_ **AppleScript** _code_. – user3439894 Aug 23 '21 at 15:58
  • Re: "If you could clarify explicitly and specifically what your needs are, I'm sure I can give you better example AppleScript code." I have several simulators on one mac OS machine and I need to execute some keystroke on one of these simulators, having UUID of the simulator. So I need to focus on the simulator with passed UUID, before keystroke executing. Thats why I want to understand how to switch focus between windows, using their ids. – Rostislav Shtanko Aug 23 '21 at 16:21
  • @Rostislav Shtanko, How are you ascertaining the `UUID`, noting that the `window id` is not a `UUID`. – user3439894 Aug 23 '21 at 16:40
  • I can make a map: Simulator UUID -> Window id, saving this information when the simulator starts. So, I need to focus on the window, using id of this window. Is it possible? – Rostislav Shtanko Aug 23 '21 at 21:28
  • RE: "I can make a map: Simulator UUID -> Window id, saving this information when the simulator starts." -- Could you provide the details of this? -- RE: "So, I need to focus on the window, using id of this window. Is it possible?" -- I want to say yes, however, do any of the _windows_ have the same _name_? If you would provide the details, to the "UUID -> Window id" that would be appreciated and then I can test the _code_ I'd write for this before posting it, thanks. – user3439894 Aug 23 '21 at 21:36