MacOS's Background Noise Feature
Posted: 2024-05-29
Tags: MacOS
Apple added a Background Noise feature in MacOS 13, but it is hidden in the Accessibility features and not easy to toggle on and off. Here is how to configure it and a Shortcut for activating/deactivating it.
Actionable Takeaways
Configure Background Sounds:
- Open the
Settings
application. - Select
Accessibility
from the navigation list on the left. - In the
Background Sounds
section:- Select the sound you want. (I like “Dark Noise”.)
- Select a volume. (I keep mine at 50%.)
- Select whether to turn off Background Sounds when the device locks. (I do.)
- Close
Settings
.
Setup a Shortcut:
- Open the
Shortcuts
application. - Click the
+
to add a new Shortcut. - Search for the
Run Shell Script
option. (You will probably be prompted to enable this in settings.) - Paste in the following script:
if [ $( defaults read com.apple.ComfortSounds "comfortSoundsEnabled" ) == "0" ] then defaults write com.apple.ComfortSounds "comfortSoundsEnabled" -bool "true" else defaults write com.apple.ComfortSounds "comfortSoundsEnabled" -bool "false" fi sleep .1 kill $(ps aux | grep '/[S]ystem/Library/PrivateFrameworks/HearingCore.framework/heard' | awk '{print $2}')
- Select
bash
in theShell:
drop-down. - Close the new Shortcut.
- Right-click it in the list and
Rename...
it to something useful. - Right-click it in the list and
Change Icon...
to whatever you would like. - Drag the Shortcut from the default
All Shortcuts
list to theMenu Bar
list. - Close
Shortcuts
.
The Script
The script is straight forward and can be found in several spots on Reddit1 or StackExchange2.
Not sure why bash
is needed on my machine. The error message from zsh
was unhelpful and it was easier/faster to just use bash
instead of debugging.
The other versions of the script use launchctl
, which is probably better, but current versions of MacOS seem to choke on all the variations of the relevant launchctl
commands to start/stop/restart the service.
To handle this problem, I am just running kill
on the service. It automatically restarts itself, so this accomplishes the same thing.
The sleep 0.1
is required to avoid an intermittent race condition that appears to be caused by defaults write
not being finalized by the time the service restarts. The result being the toggle would sometimes need to be hit several times to get it to engage or disengage. However, even a short sleep
seems to be enough to prevent this.
Finally, the [S]
in the grep
command is a trick to get grep
to not see itself in the ps
results.