I've had two reoccurring issued I needed to solve.

1. I keep forgetting to mute my computer when I go to bed. so stuff makes noise until I get up and mute it. I'm a light sleeper, and it'll wake me up or keep me up if for example my IRC apps keep clicking as people chat.

2. it's VERY annoying when I am playing a fullscreen multiplayer game and someone (almost always a telemarketer) calls my cell phone. My mac interrupts my game and tries to get me to answer the call on my computer. This takes several seconds to get rid of, and has cost us a match on several occasions.

So after passing my breaking point, I got my bash, my applesript, and my launch agents to work. Within 10 seconds of the screensaver activating, (or within an hour of the USB going inactive) a sound plays and my mac is muted. Within 10 seconds of the screensaver or USB resetting, sound is unmuted and a sound is played.

Within 10 seconds of me launching my game (WoT), Do Not Disturb is enabled and a sound effect is played. WIthin 10 seconds of me quitting the game, DND is turned off and a sound effect is played.

Here's the script if you know what to do with it. For simplicity I saved the script in my user's LaunchAgents folder, where the plist that runs it needs to be also. The agent plist is at the bottom of the script. Make changes as needed (sound effects, home folder paths) and don't forget to (A) set the script to executable, and (B) load the launch daemon (or logout/login) Sound effect files are loaded from the user's Sounds folder. DND can only be toggled, I don't know how to read it or set it to a specific state, so make sure it's off before you load this, and don't change it manually. (let me know if you figure out how to detect it's state or change it to a specific state)

Code:
#!/bin/bash

vers=2019.09.26.B
# turn on Do Not Disturb while World of Tanks is running
# also mute speaker if screen saver engine is running or USB has been idle for too long

# You have to go into System Preferences, Keyboard, Shortcuts, Mission Control, and set
# "Turn Do Not Disturb On/Off" to: command-shift-option-control-D so the script can toggle DND





####################################################################################################
###
###  CONFIGURATION
###
####################################################################################################

max_idle_sec=3600  # one hour
#max_idle_sec=12  # for testing





####################################################################################################
###
###  SOUNDS
###
####################################################################################################

        sfx_goodbye="Portal_turret_goodbye.wav"
      sfx_goodnight="Portal_turret_goodnight.wav"
          sfx_hello="Portal_turret_hello2.wav"
    sfx_hibernating="Portal_turret_hibernating.wav"
        sfx_iseeyou="Portal_turret_iseeyou.wav"
     sfx_sentrymode="Portal_turret_sentrymode.wav"
      sfx_sleepmode="Portal_turret_sleepmode.wav"
sfx_target_acquired="Portal_turret_targetaquired.wav"
    sfx_target_lost="Portal_turret_targetlost.wav"
  sfx_there_you_are="Portal_turret_thereyouare.wav"

mute_sound_file=$sfx_sleepmode
mute_sound_volume=30

unmute_sound_file=$sfx_hello
unmute_sound_volume=30

ingame_sound_file=$sfx_target_acquired
ingame_sound_volume=30

outgame_sound_file=$sfx_target_lost
outgame_sound_volume=30




####################################################################################################
###
###  FILES
###
####################################################################################################

logfile="${0%/*}/dnd.log"
enabledfile="${0%/*}/dnd_enabled.txt"




####################################################################################################
###
###  FUNCTIONS
###
####################################################################################################

########################################
##  DEBUG
########################################

debug () {
if [ $switch_debug ] ; then
  echo "DEBUG: $1" 1>&2
fi
}



########################################
##  LOG
########################################

log () {
touch "$logfile"
echo "$(date "+%Y/%m/%d %H:%M:%S") - $1" >> "$logfile"
debug "LOG \"$1\""
}



########################################
##  LOAD ENABLED
########################################

load_enabled () {
local x
if [ -f "$enabledfile" ] ; then
  x=$(cat "$enabledfile")
  if [ "$x" == "1" ] ; then
    enabled=1
  else
    enabled=
  fi
else
  enabled=
fi
debug "enabled=\"$enabled\""
}



########################################
##  SAVE ENABLED
########################################

save_enabled () {
local current saved
current=$enabled
load_enabled
saved=$enabled
debug "current enabled = \"$current\""
debug "saved enabled = \"$saved\""
enabled=$current
if [ "$saved" == "$current" ] ; then
  debug "no change to enabled \"$enabled\""
else
  echo "$enabled" > "$enabledfile"
  log "save enabled \"$e\""
fi
if [ $enabled ] ; then
  play_sound_at $ingame_sound_volume $ingame_sound_file
else
  play_sound_at $outgame_sound_volume $outgame_sound_file
fi
}



########################################
##  SET VOLUME
########################################

set_volume () {
osascript -e "set volume output volume $1"  # 0-100
}



########################################
##  GET VOLUME
########################################

get_volume () {
osascript -e 'output volume of (get volume settings)'  # 0-100
}



########################################
##  GET RUNNING
########################################

get_running () {
if (pgrep -q "World_of_Tanks") ; then
  running=1
else
  running=
fi
debug "running = \"$running\""
}



########################################
##  GET IDLE
########################################

get_idle () {
local i j
i=$(ioreg -c IOHIDSystem)
i=$(($(echo "$i" | grep  "HIDIdleTime" | head -n1 | sed 's/.* = //g')/1000000))
j=0
((j=i))
((idle_sec=j/1000))   # USB has been idle this many seconds
if [ $idle_sec -gt $max_idle_sec ] ; then
  # assume screen saver is running if USB has been idle this long (screensaver is known to crash, leaving computer locked but no screensaver running)
  idle=1
else
  idle=
fi
}



########################################
##  PLAY SOUND
########################################

play_sound () {
local f
f="$1"
if ! [ -f "$1" ] ; then
  f="${HOME}/Library/Sounds/$1"
fi
if ! [ -f "$f" ] ; then
  debug "cannot find sound \"$1\" or \"$f\" to play"
else
  debug "playing sound \"$1\" with CMD: /usr/local/bin/play \"$f\""
  #/usr/local/bin/play "$f"
  afplay "$f"
fi
}



########################################
##  PLAY SOUND AT
########################################

play_sound_at () {
local saved_volume at_volume sound_file
at_volume=$1
sound_file=$2
saved_volume=$(get_volume)
set_volume $at_volume
play_sound "$sound_file"
set_volume $saved_volume
}





####################################################################################################
###
###  SUBROUTINES
###
####################################################################################################

########################################
##  DO TEST
########################################

do_test () {
load_enabled
get_running
echo "enabled = \"$enabled\""
echo "running = \"$running\""
}



########################################
##  DO SET
########################################

do_set () {
enabled=1
save_enabled
}



########################################
##  DO CLEAR
########################################

do_clear () {
enabled=
save_enabled
}



########################################
##  DO TOGGLE
########################################

do_toggle () {
osascript -e 'tell application "System Events" to keystroke "D" using {command down, shift down, option down, control down}' ; rc=$?
if [ $rc == 0 ] ; then
  log "toggle successful"
else
  log "RC=$rc trying to toggle"
fi
}



########################################
##  DO UPDATE
########################################

do_update () {

# update do not disturb
load_enabled
get_running
get_idle
if [ "$running" == "$enabled" ] ; then
  log "running and enabled are both \"$enabled\", no change necessary"
elif [ "$running" ] ; then
  log "running but not enabled, need to set"
  do_set
  do_toggle
  log "set and turned on DND"
else
  log "not running but enabled, need to clear"
  do_clear
  do_toggle
  log "cleared and turned off DND"
fi

# update muted
m=$(osascript -e 'output muted of (get volume settings)')  # "true" or "false"
if (pgrep -q ScreenSaverEngine) ; then
  mm="true"
else
  mm="false"
fi
if [ $idle ] ; then
  mm="true"
fi
if [ "$m" == "$mm" ] ; then
  debug "screensaver and muted are both $m, no change needed"
else
  log "setting muted to \"$mm\""
  if [ "$mm" == "true" ] ; then
    play_sound_at $mute_sound_volume $mute_sound_file
    osascript -e "set volume output muted true"
  else
    osascript -e "set volume output muted false"
    play_sound_at $unmute_sound_volume $unmute_sound_file
  fi
fi

}



########################################
##  DO HELP
########################################

do_help () {
echo
echo "DND.COMMAND version $vers"
echo
echo "supported switches:"
echo
echo "-help   : display help"
echo "-toggle : toggle DND"
echo "-test   : test DND flag file and WoT state"
echo "-update : check WoT and screensaver state, toggle MUTE and DND if necessary"
echo "-set    : set DND"
echo "-clear  : clear DND"
echo
}





####################################################################################################
###
###  PARSE PARAMETERS
###
####################################################################################################

switch_debug=
while [ ${#@} != 0 ] ; do

  if [ -z "$1" ] ; then
    debug "skip blank parameter"

  elif [[ ("$1" == "-h") || ("$1" = "-help") ]] ; then
    shift
    debug "accepted switch HELP"
    do_help

  elif [[ ("$1" == "-d") || ("$1" = "-debug") ]] ; then
    shift
    switch_debug=1
    debug "accepted switch DEBUG"

  elif [[ ("$1" == "-g") || ("$1" = "-toggle") ]] ; then
    shift
    debug "accepted switch TOGGLE"
    do_toggle
    echo "TOGGLED"

  elif [[ ("$1" == "-t") || ("$1" = "-test") ]] ; then
    shift
    debug "accepted switch TEST"
    do_test

  elif [[ ("$1" == "-u") || ("$1" = "-update") ]] ; then
    shift
    debug "accepted switch UPDATE"
    do_update

  elif [[ ("$1" == "-s") || ("$1" = "-set") ]] ; then
    shift
    debug "accepted switch SET"
    do_set
    echo "SET"

  elif [[ ("$1" == "-c") || ("$1" = "-clear") ]] ; then
    shift
    debug "accepted switch CLEAR"
    do_clear
    echo "CLEARED"

  else
    echo "encountered unexpected switch \"$1\""
    exit 1

  fi
done
debug "done parsing parameters"


exit 0








# save the following plist in your user's Library/LaunchAgents folder with the file name "dnd.plist"
# and then load it with "launchctl load ~/Library/LaunchAgents/dnd.plist" (or just logout and login)


<?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>wot_dnd</string>

	<key>ProgramArguments</key>
	<array>
		<string>/Users/virtual1/Library/LaunchAgents/dnd.command</string>
		<string>-update</string>
	</array>

	<key>RunAtLoad</key>
	<false/>

	<key>KeepAlive</key>
	<false/>

	<key>StartInterval</key>
	<integer>5</integer>

</dict>
</plist>







I work for the Department of Redundancy Department