Skip to content

Instantly share code, notes, and snippets.

@eclecticpassions
Created October 12, 2023 16:05
Show Gist options
  • Save eclecticpassions/04ed3c965e4e48c8e2c1884d977c047b to your computer and use it in GitHub Desktop.
Save eclecticpassions/04ed3c965e4e48c8e2c1884d977c047b to your computer and use it in GitHub Desktop.
Shortcut for toggling AirPlay Receiver Switch in System Settings with AppleScript
Created Author GitHub Contact
2023/10/12
Naty S @eclecticpassions

Creating a Quick Action using AppleScript in Shortcuts

Why

I wanted a shortcut to quickly access the AirPlay Receiver buried in System Settings > General > AirDrop & Handoff as I often turn this off when I'm done using it. I have a gut feeling leaving it on causes issues with my M1 Mac Mini's USB-A ports' power management and sleep / wake problems.

Compatibility

This AppleScript is tested to work on macOS 14.0 Sonoma.

Steps

  1. Open Shortcuts, create New Shortcut.
  2. On the right Action Library sidebar, search for `Run AppleScript".
  3. Drag it to the left to start creating shortcut workflow.
  4. Delete the template in textbox, and paste in the script from below.
  5. Press the hammer icon to highlight syntax.
  6. Click Run (play icon) to test the workflow.
  7. Select Shortcut Details (i icon) and under details, select where you want to access this shortcut. I selected Pin in Menu Bar for quick access.
  8. Save the shortcut by closing the window. The newly created shortcut should be at the top.
  9. Rename to your liking (e.g. Airplay Receiver Switch)

Notes

  • Remember to turn on accessibility permissions for Control Center and Shortcuts in the Privacy & Security page of System Settings for the shortcut to work.
  • If this helped you,

AppleScript for toggling Airplay Receiver setting

tell application "System Settings"
	activate
	reveal pane id "com.apple.systempreferences.GeneralSettings"
	delay 0.2
end tell

tell application "System Events"
	tell process "System Settings"
		click button 4 of group 1 of scroll area 1 of group 1 of list 2 of splitter group 1 of list 1 of window "General"
		delay 0.2
	end tell
end tell

tell application "System Events"
	tell process "System Settings"
		set airDropWindow to first window whose name contains "AirDrop & Handoff"
		delay 0.2
		tell airDropWindow
			set switchGroup to checkbox 1 of group 3 of scroll area 1 of group 1 of list 2 of splitter group 1 of list 1
			if exists switchGroup then
				click switchGroup
			else
				display dialog "Switch control not found."
			end if
		end tell
	end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment