Posted on 03-06-2015 06:55 AM
Anyone have a script to toggle the Do Not Disturb option when a user logs in?
I'm open for other suggestions as well.
Solved! Go to Solution.
Posted on 03-06-2015 08:41 AM
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
Posted on 03-06-2015 08:21 AM
Would something like this work?
Posted on 03-06-2015 08:28 AM
I am looking for a way to script that if possible.
Posted on 03-06-2015 08:31 AM
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...
Posted on 03-06-2015 08:41 AM
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
Posted on 03-06-2015 10:25 AM
@elliotjordan Thank you very much! That saves us a lot of time. Just what I needed.
Posted on 04-08-2016 10:58 AM
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!
Posted on 11-23-2016 09:50 AM
@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
Posted on 01-11-2018 08:00 AM
@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.
Posted on 03-21-2018 04:15 AM
@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?
Posted on 01-22-2019 07:52 AM
@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.