Skip to content

Instantly share code, notes, and snippets.

@bebrws
Last active September 26, 2024 00:19
Show Gist options
  • Save bebrws/a0ac04b59da575f36eb104b228182c1f to your computer and use it in GitHub Desktop.
Save bebrws/a0ac04b59da575f36eb104b228182c1f to your computer and use it in GitHub Desktop.
Play Audio Notification on MacOS for Slack Notifications
#!/bin/bash
##### INSTRUCTIONS
# 1. Just place this script in your home directory, make it executable, and run it:
# curl -O https://lnkd.in/gHkrCECr https://gist.githubusercontent.com/bebrws/a0ac04b59da575f36eb104b228182c1f/raw/089c60103df44ab33df4a006aeb8e4b49a3822e0/soundOnSlack.sh
# chmod +x soundOnSlack.sh
# ./soundOnSlack.sh
#
# This will create a launch agent that will run the script every time you login.
# and it will utilize the system logging to listen for Slack notifications to play a sound passively.
# To *stop the service and remove it* you can run the generated script:
# ./stopSoundOnSlack.sh
export plist=$(
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bebrws.soundonslack</string>
<key>Program</key>
<string>$HOME/soundOnSlack.sh</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
)
export rmplist=$(
cat <<EOF
#!/usr/bin/env bash
launchctl unload "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
rm "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo "soundOnSlack.sh launchagent removed"
EOF
)
if [ -f "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist" ]; then
echo "The launchagent already exists. To remove it, run: "
echo "launchctl unload $HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo "rm $HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
else
echo "$plist" >"$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo "$rmplist" >"$HOME/stopSoundOnSlack.sh"
chmod +x "$HOME/stopSoundOnSlack.sh"
echo "Created launchagent at $HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
launchctl enable "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
launchctl load "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo "Launchagent loaded and enabled to run at startup."
echo "To disable the launchagent and remove the plist, run: "
echo "launchctl unload $HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo "rm $HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist"
echo ""
echo "OR just use the handy script at $HOME/stopSoundOnSlack.sh which will do the same thing."
exit
fi
if [ -f "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist" ]; then
export executable_name="$HOME/someOtherSlackSound.sh"
if [ -f "$executable_name" ]; then
echo "Found a custom slack sound script at $executable_name"
else
echo "If you do not like the sound used create a file at $HOME/someOtherSlackSound.sh to play whatever you want."
echo "By default it will play the sound:"
echo "afplay /System/Library/Sounds/Submarine.aiff"
fi
if [ -f "$HOME/Library/LaunchAgents/com.bebrws.soundonslack.plist" ]; then
# Use log stream to continuously monitor logs
log stream --predicate 'eventMessage contains "[create" && eventMessage contains "Starting" && eventMessage contains "Step: UserNotificationsCore.IntelligenceActor" && eventMessage contains "UserNotificationsCore" && eventMessage contains "com.tinyspeck.slackmacgap"' --info | while read -r line; do
if [ -f "$executable_name" ]; then
bash -c "$executable_name"
else
afplay /System/Library/Sounds/Submarine.aiff
fi
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment