Toggling Do Not Disturb Mavericks

sburrows
New Contributor III

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

I'm open for other suggestions as well.

1 ACCEPTED SOLUTION

elliotjordan
Contributor III

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

View solution in original post

10 REPLIES 10

elliotjordan
Contributor III

Would something like this work?

external image link

sburrows
New Contributor III

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

emily
Valued Contributor III
Valued Contributor III

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-m...

elliotjordan
Contributor III

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

sburrows
New Contributor III

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

elund
New Contributor III

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
Contributor III

@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

cgalik
Contributor

@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.

rhooper
Contributor III

@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?

luke_jaeger
Contributor

@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.