Skip to content

Instantly share code, notes, and snippets.

@jon-hotaisle
Created January 16, 2026 21:56
Show Gist options
  • Select an option

  • Save jon-hotaisle/6184b676582905eff261bb490ea67661 to your computer and use it in GitHub Desktop.

Select an option

Save jon-hotaisle/6184b676582905eff261bb490ea67661 to your computer and use it in GitHub Desktop.
I have a Mac mini connected to my TV, with HomePods as the speakers. Every time the mini goes to sleep, the audio connection drops, and I manually re-select the HomePods. This script fixes that by automatically re-selecting the AirPlay device. Hook it up to run at login (or wake), and the problem disappears. 100% vibe-coded with Claude.
#!/usr/bin/osascript
on run argv
if (count of argv) is 0 then
set deviceName to "Living Room"
else
set deviceName to item 1 of argv
end if
tell application "System Events"
tell process "ControlCenter"
-- Find and click Sound menu bar item
set soundItem to first menu bar item of menu bar 1 whose description contains "sound"
click soundItem
delay 0.8
-- Look for the window that appears
if exists window 1 then
tell window 1
tell group 1
tell scroll area 1
-- Find checkbox by AXIdentifier attribute
set allCheckboxes to every checkbox
set foundDevice to false
repeat with cb in allCheckboxes
try
set cbIdentifier to value of attribute "AXIdentifier" of cb
if cbIdentifier contains "sound-device-" & deviceName then
click cb
set foundDevice to true
exit repeat
end if
end try
end repeat
-- Close the menu
if foundDevice then
key code 53 -- Escape key
return "Successfully set output to " & deviceName
else
key code 53 -- Close menu anyway
error "Device '" & deviceName & "' not found"
end if
end tell
end tell
end tell
end if
end tell
end tell
end run
@jon-hotaisle
Copy link
Author

Built this into a simple application: https://github.com/lookfirst/LivingRoom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment