Created
January 16, 2026 21:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Built this into a simple application: https://github.com/lookfirst/LivingRoom