Skip to main content
Solved

Toggling Do Not Disturb Mavericks

  • March 6, 2015
  • 10 replies
  • 36 views

Forum|alt.badge.img+7

Anyone have a script to toggle the Do Not Disturb option when a user logs in?

I'm open for other suggestions as well.

Best answer by elliotjordan

My first draft. Feel free to steal/improve:

#!/bin/bash

# Gather the necessary variables
CURRENT_USER=$(/usr/bin/stat -f%Su /dev/console)
HARDWARE_UUID=$(system_profiler SPHardwareDataType | awk '/Hardware UUID: /{print $3}')
CURRENT_DATE=$(date -u +%FT%TZ)

# Modify the ByHost plist files
defaults -currentHost write "/Users/$CURRENT_USER/Library/Preferences/ByHost/com.apple.notificationcenterui" doNotDisturb -bool true
defaults -currentHost write "/Users/$CURRENT_USER/Library/Preferences/ByHost/com.apple.notificationcenterui" doNotDisturbDate -date "$CURRENT_DATE"

# Kill Notification Center for settings to take effect (it will automatically relaunch)
killall NotificationCenter

exit 0

10 replies

elliotjordan
Forum|alt.badge.img+12
  • Valued Contributor
  • March 6, 2015

Would something like this work?

external image link


Forum|alt.badge.img+7
  • Author
  • Contributor
  • March 6, 2015

I am looking for a way to script that if possible.


emily
Forum|alt.badge.img+26
  • Hall of Fame
  • March 6, 2015

Looks like it's user-specific, but this should get you in the right direction:
http://apple.stackexchange.com/questions/145487/how-to-enable-disable-do-not-disturb-from-shell-on-mavericks


elliotjordan
Forum|alt.badge.img+12
  • Valued Contributor
  • Answer
  • March 6, 2015

My first draft. Feel free to steal/improve:

#!/bin/bash

# Gather the necessary variables
CURRENT_USER=$(/usr/bin/stat -f%Su /dev/console)
HARDWARE_UUID=$(system_profiler SPHardwareDataType | awk '/Hardware UUID: /{print $3}')
CURRENT_DATE=$(date -u +%FT%TZ)

# Modify the ByHost plist files
defaults -currentHost write "/Users/$CURRENT_USER/Library/Preferences/ByHost/com.apple.notificationcenterui" doNotDisturb -bool true
defaults -currentHost write "/Users/$CURRENT_USER/Library/Preferences/ByHost/com.apple.notificationcenterui" doNotDisturbDate -date "$CURRENT_DATE"

# Kill Notification Center for settings to take effect (it will automatically relaunch)
killall NotificationCenter

exit 0

Forum|alt.badge.img+7
  • Author
  • Contributor
  • March 6, 2015

@elliotjordan Thank you very much! That saves us a lot of time. Just what I needed.


Forum|alt.badge.img+9
  • Contributor
  • April 8, 2016

This does not appear to be working correctly in El Capitan when trying to run it via a policy. Might anyone be able to lend a hand with this script? Thanks!


elliotjordan
Forum|alt.badge.img+12
  • Valued Contributor
  • November 23, 2016

@elund - Here's an updated version of the script that seems to work better when run as root (e.g. via Casper policy):

#!/bin/sh

CURRENT_USER=$(/usr/bin/stat -f%Su /dev/console)

cat << EOF > "/tmp/dnd_enable.sh"
#!/bin/sh
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -bool true
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "$(date)"
defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui
killall -u "$CURRENT_USER" NotificationCenter
EOF

su - "$CURRENT_USER" "/tmp/dnd_enable.sh" && rm -f "/tmp/dnd_enable.sh"

exit 0

Forum|alt.badge.img+5
  • Contributor
  • January 11, 2018

@elliotjordan Thanks for the modified script! Whenever I ran your version via policy on 10.12.6, though, it kept using "root" for $CURRENT_USER. It also wasn't deleting the script after execution, so I modified it slightly:

#!/bin/sh

CURRENT_USER=$3

cat << EOF > "/tmp/dnd_enable.sh"
#!/bin/sh
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -bool true
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "$(date)"
defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui
killall -u "$CURRENT_USER" NotificationCenter
EOF

su - "$CURRENT_USER" "/tmp/dnd_enable.sh" 

rm -f "/tmp/dnd_enable.sh"

exit 0

Seems to be working okay now in my environment, executing ongoing, triggered by login.


Forum|alt.badge.img+10
  • Valued Contributor
  • March 21, 2018

@cgalik that script worked awesomely! Thanks.
shuts down the Notifications service for 24 hrs.
Have it running during certain times(8:00 AM) and a time period of 2 weeks ... after which it should stop. Yes?


Forum|alt.badge.img+5
  • Contributor
  • January 22, 2019

@cgalik I tried the most recent version of this script on my 10.13 and 10.14 Macs -- it worked great for one day, but hasn't worked since! I have it set to run on user login. Any further suggestions for turning on "Do Not Disturb" permanently via script or config profile?

Also I assume there's a reason you wrote your script with the heredoc and the temporary dnd_enable script, instead of just executing the commands straight out of the script -- interested to know why that was necessary.