Skip to main content
deleted 2 characters in body
Source Link
Matt
  • 306
  • 2
  • 6

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 0.51
        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.

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 0.5
        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.

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.

edited body
Source Link
Matt
  • 306
  • 2
  • 6

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
            delayclick 0.5
menu item "Apple TV" of menu 1
      click menu item "Apple TV" of menudelay 10.5
        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.

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
            delay 0.5
            click menu item "Apple TV" of menu 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.

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 0.5
        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.

Source Link
Matt
  • 306
  • 2
  • 6

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
            delay 0.5
            click menu item "Apple TV" of menu 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.