Turn on Do Not Disturb While Using Certain Apps


That contains the exact same code you said fails in Post #18...

defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb

I don't recommend using Ryan Greenberg's code, because it force-quits the Notification Center and will be relatively slow.

Checking DND status and then operating the Hotkey for toggling as appropriate is a much more organic solution.


Actually, I don't quite understand what it is doing.

I had some test this morning and found out that the script does nothing else except to change the string from true to false and from false to true, regardless of the actual DND status. In other word, it does not reflect the actual DND status.

After some tests, I'm able to use some portion of the script in a macro. I tested it a few times. It seems to be working reliably.

MACRO:   DND - Toggle Do Not Disturb

DOWNLOAD Macro File:

DND - Toggle Do Not Disturb.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

Hi @martin do you use the HeyFocus app?

Here's an example of how I mute the DND, sound and internet under macOS Big Sur.


In the video you can see how the Keyboard Maestro macro (via Pushcut) also sends a notification to my iPhone to mute everything here too.

Here is the script for it:

set output to (do shell script "defaults read com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb'")
if output is "0" then
	-- start focus pushcut
	tell application "Keyboard Maestro Engine"
		do script "DCD39657-2E6E-442C-A8A3-94696FFD62B4"
		delay 0.2
	end tell
	open location "focus://focus" -- start focus
	delay 0.2
	-- start dnd
	tell application "System Events" to keystroke "d" using {command down, shift down, option down, control down}
	-- Hide Desktop Icon
	do shell script "chflags -h hidden ~/Desktop/*"
	tell application "System Events"
		set volume with output muted -- mute volume
	end tell
	do shell script "defaults write com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb' 1"
	if output is "1" then
		-- stop focus pushcut
		tell application "Keyboard Maestro Engine"
			do script "89440C73-D886-4C56-A598-55C88C5086E1"
		end tell
		-- stop dnd
		tell application "System Events" to keystroke "d" using {command down, shift down, option down, control down}
		-- Show Desktop Icon
		do shell script "chflags -h nohidden ~/Desktop/*"
		tell application "System Events"
			set volume without output muted -- unmute volume
		end tell
		do shell script "defaults write com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb' 0"
		delay 0.2
		open location "focus://unfocus" -- stop focus
	end if
end if

If you use the app Pushcut, you can also run the "All DND Mode" directly via the integrated server on the iPhone :wink:



Is @appleianer HeyFocus better than ColdTurkey?

No @hello, the features are almost identical. However, ColdTurkey is cheaper :+1:
I use the HeyFocus app through my Setapp subscription. Therefore, the price is a minor issue for me.


I do have Focus. But I have not been using it for a long time. Thanks for bringing it up.

It looks great! But I want to avoid any subscription-based app. :joy:

1 Like

I'm using a different approach. This works here in macOs Catalina.

  • System Preferences > Keyboard > Shortcuts > Mission Control > select the tickbox next to the shortcut "Turn Do Not Disturb On/Off", choose your hotkey
  • create macro that keystrokes this system shortcut
  • if I need DND state check, I use an if condition with "contains image" that check on DND icon. Will not work with fullscreen though..
1 Like

Hi Flix,

This was suggested above by @Tom:

In my macro above, I also use the hotkey to toggle DND state:


See my post above:

As for:

I think found image condition should be avoided in general and is to be used as the last resort, because:

  1. it consumes much resource when KM is looking for the image;
  2. it makes the kmmacro file much larger (this can be avoided by using a image file instead);
  3. it does not always work reliably in every situation, as you said in your post.

My macro above tries to avoid it by using the Shell script to check the DND state.

Hey friends,

I just found out, the following is sufficient for determining whether DND is on or off:

######## "0" means off, "1" means on. ########

defaults read com.apple.controlcenter 'NSStatusItem Visible DoNotDisturb'

The only possible issue we might have is this: when the hotkey is pressed to turn DND off, it takes about 5s for it to turn off completely (on my Mac, it is when the moon icon disappears from the menu bar), i.e., changing its state value to "0". In other words, after the hotkey is pressed to turn off DND, if we check the DND state within 5s, its value will still be "1", meaning its not in the off state yet.

I added a 6s pause action to ensure the status check reflects the accurate state of DND.

This solves the problem.

We can ignore this value:

defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb

Just to update here in case it might help others. I just found out that BetterTouchTool has a native action to toggle DND:


I don't know how it does it. Based on my limited tests, it works great.