Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Last active March 5, 2024 20:06
Show Gist options
  • Save sloanlance/0bd6ab31a93c4e24539e8317b1c6a46a to your computer and use it in GitHub Desktop.
Save sloanlance/0bd6ab31a93c4e24539e8317b1c6a46a to your computer and use it in GitHub Desktop.
bgsounds.sh: Toggle macOS Background Sounds on or off.
#!/bin/sh --
# Toggle macOS Background Sounds on or off.
#
# See the System Settings panel for Accessibility > Audio to change settings
# like the sound played, volume level, and automatic stop.
# `read` returns an integer, but…
if [ $(defaults read com.apple.ComfortSounds 'comfortSoundsEnabled') = 0 ]
then
newState='true'
echo 'Starting \c'
else
newState='false'
echo 'Stopping \c'
fi
echo 'Background Sounds'
# …`write` only works with Boolean value strings `true` or `false`
# Anecdotally, the sound may not start until it's been turned off, then on again
#defaults write com.apple.ComfortSounds 'comfortSoundsEnabled' -bool false
defaults write com.apple.ComfortSounds 'comfortSoundsEnabled' -bool ${newState}
defaults write com.apple.ComfortSounds 'lastEnablementTimestamp' $(date +%s)
launchctl kill SIGHUP gui/${UID}/com.apple.accessibility.heard
# add logic to test success of the `kill SIGHUP` command above
# if it fails, it may be necessary to force the service to start with the command…
#launchctl kickstart gui/${UID}/com.apple.accessibility.heard
# Or maybe that command should come BEFORE the `SIGHUP`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment