26

I would like to toggle the Do Not Disturb in the notification center with a keyboard shortcut.

I am using BetterTouchTool for some extra keyboard shortcuts but it does not support enabling/disabling the notifications in the default options.

It has an option for executing a terminal command so I'm asking here how to enable/disable Do Not Disturb from the terminal?

I found Schedule ‘Do Not Disturb’ In OS X Mountain Lion With Automator and I tried to run the commands but it didn't seem to work.

4
  • Since you're really asking about how to get it done from a keyboard, as opposed to from the shell, you should probably change the title of the question. Or if I'm wrong, please correct me!
    – webmarc
    Commented Sep 16, 2014 at 15:13
  • Actually, never mind... I didn't realize that "betterTouchTool" requires the terminal for what you want.
    – webmarc
    Commented Sep 16, 2014 at 15:21
  • I primarily asked how to do it from the shell. Doing it from the system preferences solves my case, but it's more fun to know how to do it from shell.
    – Razvan
    Commented Sep 16, 2014 at 15:23
  • Gotcha, I updated my answer with the shell info.
    – webmarc
    Commented Sep 16, 2014 at 15:24

9 Answers 9

20

You can just setup a global keyboard shortcut for it in System Preferences -> Keyboard -> Shortcuts -> Mission Control

Or if you definitely want it from the command line, an applescript to do this (assuming you setup the keyboard shortcut to use cmdshiftoptctrlD.

Note that you still MUST setup a keyboard command in System Preferences for this to work.

Put the below script into a file, say, ~/dnd.applescript

ignoring application responses
    tell application "System Events" to keystroke "D" using {command down, shift down, option down, control down}
end ignoring

Now you can run osascript ~/dnd.applescript from the command line to toggle your DND setting.

Screencap: Keyboard shortcut modification in System Preferences

10
  • 1
    Perfect! Thanks. Wish there were a better way though. Commented Dec 10, 2014 at 15:30
  • Better in what way?
    – webmarc
    Commented Dec 10, 2014 at 15:48
  • 2
    Not having to bind an obscure keyboard shortcut and toggle that with osascript. Commented Dec 10, 2014 at 15:49
  • 2
    This no longer works in Mac Mojave or Catalina. Commented Oct 3, 2020 at 23:38
  • 1
    Ventura 13.4.1: wasn't working at first but then I removed the "ignoring application responses" and saw that in my case I needed to enable iTerm in the Privacy & Security > Accessibility menu.
    – AbePralle
    Commented Aug 5, 2023 at 15:42
23

You can simplify the answer that razvanz provided by using the -currentHost argument to the defaults command.

Enable Do Not Disturb:

defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "`date -u +\"%Y-%m-%d %H:%M:%S +0000\"`"
killall NotificationCenter

(via https://heyfocus.com/blog/enabling-do-not-disturb-mode/)

Disable Do Not Disturb:

defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false
killall NotificationCenter

Now you could easily wrap this up as a script to enable or disable "Do Not Disturb" as a script that would work on anybody's machine regardless of system preferences. Here is an example of how to do that:

#!/bin/bash

set -eou pipefail

# From https://heyfocus.com/enabling-do-not-disturb-mode and
# https://apple.stackexchange.com/questions/145487

if [[ $(defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb) -eq 0 ]]; then
  defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true
  defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "`date -u +\"%Y-%m-%d %H:%M:%S +000\"`"
  killall NotificationCenter
  echo "Do Not Disturb is enabled. Run $0 to turn it off (OS X will turn it off automatically tomorrow)."
else
  defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false
  killall NotificationCenter
  echo "Do Not Disturb is disabled. Run $0 to turn it on again."
fi

Source: https://gist.github.com/ryangreenberg/5267f68a8e7b07ea66370b4eb5580ab9

6
  • 2
    Minor visual bug! [Noted for folks who look @ color]: I tried this and it sorta works. I snoozed some notifications for 5 mins, and then turned on Do Not Disturb for Notifications, and then ran the commands under "Disable Do Not Disturb". On the one hand, the icon remained greyed out, but on the other, it did appear to work otherwise - notifications popped up in a few minutes. Second test: I turned on Do Not Disturb for Notifications (w/o snoozing anything), & ran the commands under "Disable Do Not Disturb". The icon remained greyed out, but notifications popped right up. Commented Feb 23, 2018 at 16:19
  • 1
    @MatthewElvey That might be a change from Sierra to High Sierra. We probably need to find a new way to reload the menubar icon.
    – Ryan
    Commented Mar 5, 2018 at 23:57
  • 4
    Dnd doesnt appear to be changed on high sierra/mojave with this solution Commented Mar 9, 2019 at 23:09
  • +1 After going down a ridiculous number of rabbit holes, this works on macOS Mojave 10.14.6. Not sure why, but I had to change the first line to set -eu -o pipefail to get it to work properly.
    – ssent1
    Commented Sep 5, 2020 at 3:59
  • 1
    I am using Catalina. This solution will enable DND, but does not disable DND. Commented Oct 3, 2020 at 23:43
18

As of OS X 10.10.3, this AppleScript will toggle "Do Not Disturb." No keyboard shortcut required:

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

You can save it as an AppleScript and run it from the terminal with osascript DoNotDisturb.applescript, or you can include it in a Bash script by wrapping it in in a heredoc like so:

#!/bin/bash
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
2
  • 7
    This means that you can also toggle DoNotDisturb by clicking the rightmost tiles in the menu bar while holding the Option key.
    – Razvan
    Commented May 12, 2015 at 17:18
  • 5
    This no longer works on Mojave. Applescript seems to click the menu bar item as if option was not down, even though it is Commented Mar 14, 2019 at 20:21
7

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
1
  • 3
    This no longer works on Mojave. Applescript seems to click the menu bar item as if option was not down, even though it is Commented Mar 14, 2019 at 20:22
5

Schedule Do Not Disturb

Just to add that you can also schedule Do Not Disturb from the command line to activate/deactivate each day at set times.

To set the time when DND will be enabled:

defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui dndStart -integer <start_time_in_minutes>

To set the time when DND will be disabled:

defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui dndEnd -integer <end_time_in_minutes>

NOTE: replace <start_time_in_minutes> and <end_time_in_minutes> with the desired value (explained below).


Example:

To schedule DND to start each day at 15:00 and to end at 18:30 do the following:

Convert 15:00 and 18:30 to minutes to get the value of <start_time_in_minutes> and <end_time_in_minutes>. That is, multiply the number of hours by 60 and add the number of minutes.

For 15:00 that would be: 15 * 60 + 0 = 900 and for 18:30 that would be: 18 * 60 + 30 = 1110. Giving us the commands below:

defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui dndStart -integer 900
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui dndEnd -integer 1110
killall NotificationCenter # 'resets' Notificatio Center so that it reads the DND change
1

Building on James's answer, I'd also liek to point out that you can also determine if it is turned on or off. So the following turns it off, and does nothing if already off:

if [[ $(plutil -convert xml1 -o - ~/Library/Preferences/ByHost/com.apple.notificationcenterui.000-000-000-000.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

You must substitute your own machine GUID into the filename (there is only one file there, so it's easy to figure out)

1
  • 2
    Unfortunately this seems to no longer work in Big Sur -- the file always contains false regardless of whether notification center is enabled. Commented Sep 29, 2020 at 17:32
1

Answer as of 2020-10, Version 10.15.6

https://github.com/sindresorhus/do-not-disturb-cli

After installing with
$ npm install --global do-not-disturb-cli

You can toggle Do Not Disturb by running
$ dnd


I was searching for a way to set an alias in my bash profile that would

  1. Enable Do Not Disturb
  2. Un-Hide the Dock

So now I can do

alias dndon='dnd on && defaults write com.apple.dock autohide -bool false && killall Dock'

alias dndoff='dnd off && defaults write com.apple.dock autohide -bool true && killall Dock'
0

This comes up as a Google hit, so I thought I'd update it with a working, command-line, nothing-to-learn, Mavericks-friendly solution as of 2021-07-28: https://github.com/joeyhoer/dnd. Downloaded it and ran it and it just works, including changing icon status. Hours of searching has ended. My system has now improved!

0

Here is something that works on Big Sur (as most answers do not):

brew install vitorgalvao/tiny-scripts/calm-notifications
calm-notifications on

Taken from this answer: https://github.com/sindresorhus/do-not-disturb/issues/9#issuecomment-765083970

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .