'Open Screen Mirroring in Control Center for Sidecar?
OS & Program Details
- macOS Monterey (12.3+): This is only applicable to 12.3 or above since the method of activating/deactivating Sidecar changed a bit with the introduction of Universal Control (rather than being in the CC Display module, the button for Sidecar got moved into Screen Mirroring)
- Device: M1 MacBook Pro 13-inch (Late 2020). Not sure if this matters, but thought it'd be helpful to include just in case.
- Application: Building and running in the built-in
Script Editor.app
Problem
So going off of this link, I am trying to build an AppleScript that starts/ends a Sidecar connection, and achieving this through GUI scripting the Control Center (rather than System Preferences, or by including the Screen Mirroring menu bar item).
I seem to have part of it down, but am not able to actually click the Screen Mirroring button (or checkbox, as it's classified in AppleScript); it does nothing. Here's the code I have so far:
set deviceName to "iPad"
set sysVer to system version of (system info) as real
tell application "System Events"
tell its application process "ControlCenter"
activate
-- Click the Control Center menu and give it time to draw
click menu bar item "Control Center" of menu bar 1
delay 1
if sysVer ≥ 12.3 then
-- Get the Screen Mirroring "checkbox" and click it
set screenMirroringToggle to (checkbox 1 of window "Control Center" whose title is "Screen Mirroring")
click screenMirroringToggle
-- Do stuff that gets the iPad button to start/end Sidecar
else
-- Do stuff for other versions of macOS Monterey or Big Sur
end if
end tell
end tell
Things I've Tried
I am still a little new to AppleScript, so I tried building the script in iterations. I first tried getting all the possible checkboxes in the CC with this:
if sysVer ≥ 12.3 then
-- Get all checkboxes in the Control Center menu
set ccCheckboxes to title of (every checkbox of window "Control Center")
return ccCheckboxes
end if
That returns this list (Link keyboard and mouse is not what I'm looking for — that is for Universal Control, not Sidecar):
{"Wi‑Fi", "Focus", "Bluetooth", "AirDrop", "Screen Mirroring", "Link keyboard and mouse", "Airplay Audio"}
Setting the Screen Mirroring checkbox throws no errors (and can be returned), but the click command doesn't (appear to) do anything with this:
set myToggle to (checkbox 1 of window "Control Center" whose title is "Screen Mirroring")
click myToggle
Just to make sure the click command actually does something, I tried it with the AirDrop checkbox via:
set myToggle to (checkbox 1 of window "Control Center" whose title is "AirDrop")
click myToggle
This works as expected; the AirDrop icon in the CC is toggled (switching between "Contacts Only" and "Off"). I then went ahead and tried it with every other checkbox returned in ccCheckboxes
above, and everything but Screen Mirroring works: all toggles (Wi-Fi, Focus, Bluetooth, AirDrop) switch between on and off, and the rest (Link keyboard and mouse, Airplay Audio) open their secondary window. Am I doing something wrong or is this just a bug?
Solution 1:[1]
I use ipad pro as my m1 mac mini's main display ,after I upgrated to mac os 12.3(beta)a few moths ago , the old applescript did't work any more, I got the same idea like you do, also I knew little applescripts, and I find a way to activate sidecar by just click the coordinate of your ipad 's name. because I use this script with mac mini,I also add a few code to check whether the mac mini is connected to a monitor, if it's connected to a monitor this script do nothing, if not this script will click Screen Mirroring, then click the coordinate of your ipad's name and turn ipad as mac mini's main display. to get the coordinate of your ipad without a display I also need to install betterdummy to create a 4:3 virtual display? you can get the coordinate by hold cmd shit 4,if you use mac book you don't need betterdummy. this is a link to my script, this script worked,but it works with language chinese,you need to change a little bit .
if application "??????" is running then
tell application "System Events"
set string1 to display name of desktop 1
set string2 to missing value
if string1 is equal to string2 then
activate application "BetterDummy"
delay 3
tell application "System Events" to tell process "SystemUIServer"
set bt to menu bar item "????" of menu bar 1 of application process "ControlCenter" of application "System Events"
click bt
set xy to position of bt
set x to (item 1 of xy)
set y to (item 2 of xy)
end tell
tell application "System Events"
delay 1.5
click at {x - 150, y + 115}
click bt
end tell
else
end if
end tell
else
tell application "System Events"
set string1 to display name of desktop 1
set string2 to missing value
if string1 is equal to string2 then
activate application "BetterDummy"
delay 3
tell application "System Events" to tell process "SystemUIServer"
set bt to menu bar item "????" of menu bar 1 of application process "ControlCenter" of application "System Events"
click bt
set xy to position of bt
set x to (item 1 of xy)
set y to (item 2 of xy)
end tell
tell application "System Events"
delay 1.5
click at {x - 150, y + 115}
click bt
end tell
else
delay 1
tell application "System Events" to tell process "SystemUIServer"
set bt to menu bar item "????" of menu bar 1 of application process "ControlCenter" of application "System Events"
click bt
set xy to position of bt
set x to (item 1 of xy)
set y to (item 2 of xy)
end tell
tell application "System Events"
delay 1.5
click at {x - 150, y + 115}
click bt
end tell
end if
end tell
end if
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eric Aya |