Turn on Do Not Disturb While Using Certain Apps

Hi @jessestarrphoto, it doesn't work for me under macOS Mojave either.
But you can try the macro of @ccstone:

Hey @jessestarrphoto,

What happens when you manually Option-Click the Notification Icon in the Menu Bar?

On macOS Sierra10.12.6 this will toggle the DND status On/Off.

Mojave has played havoc with macOS automation in a variety of respects in the name of security.

Try this macro:

Option-Click Notification Icon v1.00.kmmacros (6.4 KB)

If it works then you should be able to incorporate it into the other macro.

-Chris

Well, the script didn't work but I did figure out a way to use the 'Click at' action to make this work using an Option-Click. So, thanks for the help!

In MacOS Mojave 10.14.6 macro doesn't work :frowning:

hello @jessestarrphoto

would you be able to share the updated macro that works for Mojave?

Thank you!

To make Onan’s macro work again with macOS Mojave:

1​) Set a shortcut to toggle DND in System Preferences > Keyboard > Shortcuts:

13-pty-fs8

2​) In @Onan’s macro from this post, just replace each of the two AppleScript actions with a Type Keystroke action:

40-pty-fs8

Obviously, the keystroke must correspond to the shortcut you’ve set in System Preferences.

With this modification the macro works fine for me under Mojave 10.14.6.

5 Likes

So helpful for pandemic-era teaching (I'm triggering w/ zoom and Camtasia).

@Tom's Post #9 is the simplest solution to this puzzle.

But – it's handy to be able to check the status of Do Not Disturb.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2021/09/10 08:21
# dMod: 2021/09/10 08:21 
# Appl: Miscellaneous AppleScript
# Task: Get the Status of Do Not Disturb
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @DND, @Do_Not_Disturb, @Status
# Test: macOS 10.14.6
# Vers: 1.00
--------------------------------------------------------

set dndStatus to getDND_Status()

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on getDND_Status()
   set shCmd to "defaults -currentHost read com.apple.notificationcenterui doNotDisturb"
   set dndStatus to ((do shell script shCmd) as number) as boolean
   return dndStatus
end getDND_Status
--------------------------------------------------------

Get Status of Do Not Disturb v1.00.kmmacros (7.1 KB)
Keyboard Maestro Export

Hey Chris, I'm getting the following error with this macro (I had to change the AppleScript settings to disable failure aborts as well as to include errors)

/var/folders/39/3230gggn6y3bxl038l0yv_xh0000gn/T/Keyboard-Maestro-Script-0794332E-1B64-4185-BBA7-46EA027B65FC:741:762: execution error: 2021-09-10 10:41:54.463 defaults[16371:159711]

The domain/default pair of (com.apple.notificationcenterui, doNotDisturb) does not exist (1)

Well, puddles...

You're on Big Sur?

What happens when you run this in Script Debugger?

set checkDNDstatusCMD to ¬
   {"defaults -currentHost read", space, ¬
      "~/Library/Preferences/ByHost/", ¬
      "com.apple.notificationcenterui", ¬
      space, "doNotDisturb"} as text

set statusOfDND to (do shell script checkDNDstatusCMD) as number as boolean

Yep, Big Sur 11.5.2

SD gives me the following error

2021-09-10 11:46:11.899 defaults[34156:327801]
The domain/default pair of (/Users/cdthomer/Library/Preferences/ByHost/com.apple.notificationcenterui, doNotDisturb) does not exist

Hi Chris,
I can run it in ScriptDebugger without error. But I'm always getting the false result regardless of my DND status.

Okay – that doesn't shock me too much – since Apple has made many changes since Mojave.

I expect the defaults domain is different for Big Sur.

-Chris

Hey Martin,

You're on Mojave – right?

Hmm...

It works for me whether I Opt-Click the menu icon or change the DND status in the Notification Center Slide-Out Sheet.

What localization of the macOS are you using?

-Chris

I'm on macOS: Big Sur (11.5.2).

Strangely, when I run the below code, the result is always true regardless of my DND status.

My best guess is that you upgraded your system, and while this setting is no longer used it is still seen by your system but no longer settable.

I could be waay off though.

-Chris

This code works:

#!/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:

Hmm...

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.

-Chris

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.

Video

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"
else
	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:

Video

3 Likes