MacOS – Randomize Alert Sound Effect in Mac OS

macos

Is there any way to randomize the alert sound effect in Mac OS? In system prefs I am able to select which sound effect I want to hear. Is there a way for the system to choose a random sound each time?

Best Answer

This isn't built into macOS so this is something you are going to have to script and do a bit of customization. You will need the following:

  • Custom location for your sound
  • Script to randomize the sound
  • launchd plist to run the script at a set interval

Custom Location for Sound

System sounds are in the /System/Library/Sounds directory which I don't recommend attempting to modify. You can copy them to ~/Libary/Sounds or another custom directory of your choosing. This way, you can add your own custom sounds without interfering with the System sounds themselves.

Script to Randomize Soundfile

The script below will generate a random number between 0 and the number of files you have in your sound directory. It will then create an array of all the file names and then select a random filename (array element) which it will then copy that file to another file called CustomAlert.aif. You can customize the filename and path to suit your needs.

#!/bin/bash
#
# script filename:  randomAlert.sh

#User Defined Variables
  sdir="/Users/foobar/test"
  sfname="CustomAlert.aif"


#Remove Custom Alert Sound if already Exists

  if [ -f ${sdir}/${sfname} ]; then
    rm -f ${sdir}/${sfname}
  fi



#Get Number of Files in Directory
  numfiles=(*)
  numfiles=${#numfiles[@]}


#Generate Random Number
  rnum=$(( $RANDOM % ${numfiles}  + 0 ))


#Put names of files into array
  fnames=(*)

#Copy file to Alert Sound File
  cp ${sdir}/${fnames[${rnum}]} ${sdir}/${sfname}

exit

Make sure the script's permissions is set to allow execution:

chmod +x randomAlert.sh

Finally, make sure the script is in a "safe" location (a folder in your home directory is always a good place)

Launchd plist

To have the script run, you need to use the launchd service. The plist can go in any one of 3 locations:

  • ~/Library/LaunchAgents - For just one user, run as the user (i.e. yourself)
  • /Library/LaunchAgents - For all users, run as the user
  • /Library/LaunchDaemons - As a system daemon (run as root)

I recommend either of the first two; it doesn't make sense to run as a system daemon.

<?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.user.randomSound</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/USERNAME/Path_to_Scripts/randomAlert.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
    <dict>
        <key>Hour</key>
        <integer>00</integer>
        <key>Minute</key>
        <integer>00</integer>
    </dict>

</dict>
</plist>

Once you have copied it to the appropriate directory, you need to launch it with launchctl

launchctl load com.user.randomSound.plist

Set your Alert Sound to the Custom File

In your settings, set the alert to CustomAlert.aif. Even though the sound will change, the name of the file will stay the same.

That's it! Every 24 hours, your script will kick off and set a new random alert sound.