Building on answers from James and Zsolt, I've created a couple of scripts to turn on or off (not toggle) DND state. They also don't require any key bindings or machine GUID to work.
IMPORTANT: Please note that running these scripts for the first time may require accessibility permission for the app which runs the scripts. If you do not grant the permission in the request the alt/option button will remain pressed for the system and you will need to log out and back in to "unpress" it. This is also true for previous answers with AppleScript. If the script is edited, the permissions will need to be revoked and re-granted. Permissions are granted using:
System Preferences > Security & Privacy > Accessibility > Add your app
For macOS Sierra and High Sierra it's menu bar 1
:
Turn Do Not Disturb ON (disable notifications):
if [[ $(plutil -convert xml1 -o - ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist | grep false) ]]; then
osascript <<EOD
tell application "System Events" to tell process "SystemUIServer"
key down option
click menu bar item 1 of menu bar 1
key up option
end tell
EOD
fi
Turn Do Not Disturb OFF (enable notifications):
if ! [[ $(plutil -convert xml1 -o - ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist | grep false) ]]; then
osascript <<EOD
tell application "System Events" to tell process "SystemUIServer"
key down option
click menu bar item 1 of menu bar 1
key up option
end tell
EOD
fi
For earlier versions of macOS it's menu bar 2
:
Turn Do Not Disturb ON (disable notifications):
if [[ $(plutil -convert xml1 -o - ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist | grep false) ]]; then
osascript <<EOD
tell application "System Events" to tell process "SystemUIServer"
key down option
click menu bar item 1 of menu bar 2
key up option
end tell
EOD
fi
Turn Do Not Disturb OFF (enable notifications):
if ! [[ $(plutil -convert xml1 -o - ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist | grep false) ]]; then
osascript <<EOD
tell application "System Events" to tell process "SystemUIServer"
key down option
click menu bar item 1 of menu bar 2
key up option
end tell
EOD
fi