Scripting the Unscriptable: Screen Sharing in macOS Sonoma
macOS Sonoma’s new Screen Sharing app comes with a cool “High Performance” mode for Apple Silicon Macs. It looks great, but I have a gripe. For the longest time, I’ve had a single-serve Alfred workflow that opens a Screen Sharing connection to my home server (an M1 Mac mini) using this shell script:
open vnc://[email protected]
This is what Merlin Mann would call a no look pass. I use it all the time for quick in-and-out maintenance jobs and, in previous versions of macOS, the Screen Sharing app would immediately open a new connection. No questions asked.
The Problem
When I open a Screen Sharing connection with the vnc://
URI scheme in Sonoma, Screen Sharing now gives me this prompt…
This dialogue appears every time you open a new connection and it does not remember your choice. Switching to “High Performance” mode currently requires me to mouse over and click the radio button myself. I probably did that half a dozen times before I gave up and just started hitting return to shoo away the dialogue. That means I almost always run Screen Sharing in “Standard” mode which is a real pity.
Scripting the Unscriptable
As far as I can tell, there’s no officially-supported way to programmatically start a “High Performance” connection. So hold my beer… We’re doing UI scripting.
The main headache with UI scripting is extracting information from macOS about which elements are there to be scripted. Without information about an app’s accessibility structure, there’s no hope of gluing together an AppleScript with the right tell
statements. There are a few helpful utilities out there…
- Accessibility Inspector is an Apple utility that comes with Xcode. I am not installing Xcode just for the purposes of this scripting task, so I’ll have to pass this time.
- UI Browser by Bill Cheeseman used to be the perfect tool for the job but it was retired in 2022. There’s an early build of a Swift re-write out there but it’s clearly unfinished work.
- UIElementInspector is an ancient sample project provided by Apple to demonstrate use of the Accessibility API (a pre-built version of the project comes in the .zip file). It shows some of the required data but not in a very user-friendly way. Pass.
- Chris Stone (of Keyboard Maestro Forum fame) wrote an AppleScript that dumps the GUI scripting information for the frontmost window of the active application.
I ended up using Chris Stone’s script because it’s free and I could get it working immediately. This is the output it produced for the Screen Sharing dialogue…
application Process "Screen Sharing"
window
group 1
UI element 1
radio group 1
radio button 1
radio button 2
button 1
button 2
button 3
static text 1
After a bit of wrangling with Script Editor, this is where I ended up…
tell application "System Events"
tell process "Screen Sharing"
-- replace with your target computer's details
open location "vnc://[email protected]"
-- wait for UI to load
activate
repeat until (count of windows) = 1
delay 0.05
end repeat
-- click "High Performance"
click radio button 2 of radio group 1 of UI element 1 of window 1
-- click "Continue"
click button 3 of group 1 of window 1
end tell
end tell
Bingo. I still get a brief flash of the mode selection screen but at least it no longer stops me in my tracks. It’s a crying shame that nobody within Apple had the time (or the inclination) at add proper scriptability to this otherwise-great upgrade.
Maybe in macOS 15.