0

I want to start Screen Mirroring from my MacBook to my AppleTV with a keyboard shortcut. I used Automator's Recording function ("Watch Me Do") to record the three necessary clicks (open Control Center, click on Screen Mirroring, click on Apple TV). This is the resulting code:

-- Click the “Control Centre” menu bar item.
delay 0.608482
set timeoutSeconds to 2.0
set uiScript to "click menu bar item 2 of menu bar 1 of application process \"Control Centre\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “<fill in title>” button.
delay 0.84342
set timeoutSeconds to 2.0
set uiScript to "click UI Element 6 of group 1 of window \"Control Centre\" of application process \"Control Centre\""
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “<fill in title>” button.
delay 1.017773
set timeoutSeconds to 2.0
set uiScript to "click UI Element 2 of group 1 of window \"Control Centre\" of application process \"Control Centre\""
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script "tell application \"System Events\"
" & uiScript & "
end tell"
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout

Now the problem is that when I execute that script, in the second step it's not Screen Mirroring that's being clicked but Focus, the element above it. So somehow the line set uiScript to "click UI Element 6 of group 1 of window \"Control Centre\" of application process \"Control Centre\"" doesn't work as expected. I tried to replace the UI Element 6 with other numbers. Most of them result in a click on Focus, others turn off Bluetooth or AirDrop ...

How can it be that a recorded action yields a different result when played back? Any ideas? I'm on macOS Ventura btw.

joschaf
  • 515
  • 2
  • 6
  • 14
  • I have tried a script on Monterey and the Screen Mirroring checkbox didn't appear to be clickable via script. Might be worth a try using an external clicker like cliclick or keyboard maestro. – user3579815 Nov 14 '22 at 01:12

1 Answers1

-1

Edit: Solution for Ventura 13.0.1

After some testing on a Ventura VM, I found that a lot of stuff changed between versions so what I posted initially as a potential solution won't work.

In your script change set uiScript to "click UI Element 6 of group 1 of window \"Control Centre\" of application process \"Control Centre\""

to

set uiScript to "perform action 1 of UI Element 6 of group 1 of window \"Control Centre\" of application process \"Control Centre\""

Worked for me on Ventura (I just had to change the spelling of "Centre" to "Center") but that is probably because we might have different system languages set.

This other thread describes some of the changes on Ventura for this problem.


I've had a lot of issues with applescript and interacting with the Screen Mirroring item in the Control Center dropdown. As well as, trying to use Automator's record function to do something similar. The Screen Mirroring item in the Control Center dropdown is technically a checkbox so a "click" action should work on it but it doesn't for some reason. While the same "click" action seems to work on other items in the same dropdown like "focus" or "bluetooth". The issue also seems to exist on Big Sur and Monterey. I found that you need to "perform action 2" (Monterey) and "perform action 1" (Big Sur) in order to "click" on it from Control Center dropdown.

I'm not on Ventura but this works for me on Monterey.

set uiScript to "perform action 2 of UI Element 6 of window \"Control Center\" of application process \"ControlCenter\""

Things change between system versions so I had to remove the "of group 1" part to get this line to run on my system. Also had to change the spelling of "Control Centre" (not sure why spelling is different) to "Control Center" and removed the space in "Control Center" on the "of application process" part.

With your current code this might work. I changed the "click" to "perform action 2" and instead of selecting UI Element by number I used the name. I left the spelling the same of "Control Centre" though.

set uiScript to "perform action 2 of UI Element \"Screen Mirroring\" of group 1 of window \"Control Centre\" of application process \"Control Centre\""

It's possible that "perform action 2" might not even work on Ventura, as it changed from "perform action 1" on Big Sur so it could have changed again.

Here is a snippet of a working solution I have for Big Sur and Monterey. I have added a section that might work for Ventura. From what I gather from your code it looks like Screen Mirroring is inside a "group 1" which was different from Monterey.

Edit: This won't work for ventura

tell application "System Events"
tell its application process "ControlCenter"
    click menu bar item "Control Center" of menu bar 1
    tell its window "Control Center"
        -- Monterey
        if exists checkbox "Screen Mirroring" then
            tell its checkbox "Screen Mirroring"
                perform action 2
                delay 1
            end tell
        else
            -- big sur
            -- "action 1" instead of "action 2"
            if exists (checkbox "Screen Mirroring" of group 1 of group 1) then
                tell its checkbox "Screen Mirroring" of group 1 of group 1
                    perform action 1
                    delay 1
                end tell
            end if
            -- ventura??
            -- "Screen Mirroring" is inside a "group 1"
            if exists (checkbox "Screen Mirroring" of group 1) then
                tell its checkbox "Screen Mirroring" of group 1
                    perform action 2
                    delay 1
                end tell
            end if
        end if
    end tell
end tell
end tell

I'm hoping not too many things have changed between Monterey and Ventura and it'll work. Run it from the system script editor to test.

If you'd like to see my full example please go to github here. The script also is able to select a device from the Screen Mirroring dropdown. I just got a solution working for Big Sur and Monterey about a week ago after much frustration and testing.

kconsiglio
  • 401
  • 1
  • 8
  • Thank you so much for the effort to help, I appreciate it! And your edit (`perform action 1 of UI Element 6`) worked indeed for the second click (Screen Mirroring). Unfortunately the third step (click on AppleTV) doesn't work either even though it's the exact code that was the result of the recording :( With the code `click UI Element 2 of group 1 of window \"Control Centre\" of application process \"Control Centre\"` (or `perform action 1 of ...`), the item "Display Settings..." gets selected, which is _underneath_ my AppleTV and the iPad. How I find out which menu item number a menu item is? – joschaf Nov 18 '22 at 19:40
  • @joschaf I'm running Ventura in a VM which doesn't seem to support screen mirroring as no devices are showing up in the dropdown. So unfortunately my testing past this point is limited. Could you try `set timeoutSeconds to 0.5 set uiScript to "set x to UI Elements of its scroll area 1 of group 1 of window \"Control Center\" of application process \"Control Center\"" my doWithTimeout(uiScript, timeoutSeconds)` before the line with `click UI Element 2`.Run in script editor and look at the bottom Replies tab. See if there is any output in there. On Monterey the devices are in that scroll area – kconsiglio Nov 19 '22 at 01:25
  • The output is `--> button 1 of group 1 of window "Control Centre" of application process "ControlCenter“` (this is the "Display Settings…" button at the bottom). If I change `click UI Element 2 of group 1 …` to `click UI Element 1 of group 1 …`, the output is `--> static text "Screen Mirroring" of group 1 of window "Control Centre" of application process "ControlCenter"` (the headline at the top of the window). And my devices are listed in _between_ those two UI elements, under the subheading "Mirror or extend to:" – joschaf Nov 19 '22 at 07:33
  • @joschaf so it looks like the devices aren't in the scroll area but can be seen from the second command you did. It's promising that we can see the device names. On Monterey something like this works:`click UI element "my device" of UI Element 1 of group 1 ...` – kconsiglio Nov 20 '22 at 02:25