1

The new macOS Airplay Receiver service uses port 5000. I'd like to temporarily disable it during the running of a script. This service is manually toggled by an option under System Preferences > Sharing > Services > Airplay Receiver. The process that seems to run the service is /System/Library/CoreServices/ControlCenter.app/Contents/MacOS/ControlCenter.

I've looked into the defaults command and some osascript options but been unable to find the right incantations. The ideal solution resembles:

#!/bin/bash
toggle_airplay_receiver off
echo "Running my script and doing stuff on port 5000..."
toggle_airplay_receiver on
echo "All done, AirPlay Receiver works again :)"

Ideally there's a direct way to do start and stop the service, but a janky method of scripting the System Preferences gui is also acceptable.

1

but a janky method of scripting the System Preferences gui is also acceptable.

The example AppleScript code, shown below, was tested as a shell script under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Example AppleScript code:

#!/bin/bash

osascript <<EOS
tell application "System Preferences" to reveal pane id "com.apple.preferences.sharing"
tell application "System Events" to tell window 1 of application process "System Preferences"
    repeat until exists checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver")
        delay 0.1
    end repeat
    if value of checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver") as boolean then
        click checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver")
    end if
end tell
tell application "System Preferences" to quit
EOS

echo "Running my script and doing stuff on port 5000..."

osascript <<EOS
tell application "System Preferences" to reveal pane id "com.apple.preferences.sharing"
tell application "System Events" to tell window 1 of application process "System Preferences"
    repeat until exists checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver")
        delay 0.1
    end repeat
    if not value of checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver") as boolean then
        click checkbox 1 of (first row of table 1 of scroll area 1 of group 1 whose value of static text 1 is "AirPlay Receiver")
    end if
end tell
tell application "System Preferences" to quit
EOS

echo "All done, AirPlay Receiver works again :)"

Notes:

This requires Terminal be added to System Preferences > Security & Privacy > Privacy > Accessibility and initially replying to the “Terminal” wants access to control “System Events”. Allowing control will provide access to documents and data in “System Events”, and to perform actions within that app. dialog box.

As coded it assumes System Preferences is initially closed and not opened to a modal pane, otherwise additional coding is necessary.

1
  • You really know your osascript :)
    – AJ Slater
    Dec 7 '21 at 20:31

You must log in to answer this question.

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