Turn on Do Not Disturb While Using Certain Apps

I'd like to have Do Not Disturb enabled when I'm using either of two apps that I use for presenting to clients because I don't want a text message to pop up during a meeting.

I've set a keyboard shortcut to toggle DND in the Mac system preferences but I'm struggling to figure out the series of triggers and conditions that will turn it on when I launch one of the apps and turn it off when quit the app. I need to make sure that it doesn't turn off until both apps are quit.

Thanks!

Here is a macro that will do what you want automatically. When specific applications are launched, Do Not Disturb is enabled. When any of the specific applications are quit, it will only disable Do Not Disturb if all of the applications are not running.

In this macro, I have configured Safari and App Store as the applications. Obviously you would change these to the apps you wish, but just make sure to change both locations. There is a comment in the macro explaining it in more details.


Toggle DND when running specific apps.kmmacros (10.3 KB)

Macro Image

image

4 Likes

Thank you very much. I've adjusted the macro to reflect my apps but it seems that there is something broken. When I open one of the apps, the Notification Center opens but DND does not get turned on... Do you know if this works with Mojave?

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: