1

In System Preferences > Displays, bottom left corner is "Add Display". I can choose another (Monterey+) Mac, Apple TV, iPad, possibly other things, to effectively become an additional display for my Mac. It's pretty cool.

I want to automate connecting and disconnecting to one of those. Either AppleScript or bash is fine. I'd really prefer not to use GUI scripting if possible, but if it's the only way I'll take that over no solution.

An anyone say definitively if this is possible, and/or how to do it?

Thanks!

1 Answer 1

0

I don't think a solution purely in bash would be possible, since we are trying to interface with internal macOS application events. Further, I could not find direct reference to AirPlay, Displays, or Mirroring within the Script Editor.app's Dictionary (open Script Editor.app, File → Open Dictionary...). But this still leaves the option of GUI scripting via AppleScript, which I will offer below.

In macOS 11+, this old example syntax exemplified here won't work because of the UI changes that have been made since macOS 11. It may be possible to modify it for the new Control Center Menu Bar item, as was done successfully for a checkbox selection type in the second example here, but I haven't been able to adapt it successfully for Screen Mirroring. Others have also been attempting this.

In the meantime, it can still be accomplished with some AppleScript GUI scripting via the System Preferences route, as apposed to going through the system Menu Bar's Control Center:

tell application "System Preferences"
    quit
end tell
tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.displays"
end tell
tell application "System Events"
    tell application process "System Preferences"
        repeat until exists window "Displays"
        end repeat
        tell pop up button 1 of window 1
            click
            click menu item "Apple TV" of menu 1
            delay 1
        end tell
    end tell
end tell
tell application "System Preferences"
    quit
end tell

Be sure to hardcode the name of your device in place of "Apple TV". Further, you could pass an argument to toggle AirPlay mirroring for devices on the fly with a proper Automator Automation, or if you wrap this script into a shell script or a bash alias.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .