Macro for "Turn on Do Not Disturb until...."

It’s such a sad thing that Apple has not yet enhanced “Do Not Disturb” that you may have “do not disturb for next …(length of time) / until…(a time).” I’m wondering if anyone knows if KM can make this operation possible (probably with help of applescript?)

e.g. this would be fantastic –

  1. hotkey triggers a dialogue box asking things like “I want to turn on DND for next…/until…” – where you may type the length of time.
  2. KM would send a notification/dialogue box when the time runs up, offering yes/no button to confirm turning off DND or just continue (there can be more tricks played here such as "prolong DND for another…(time), but anyway its optional) and KM would operate according to your answer.
  3. Even if OSX doesn’t open the API for DND, we can still assign hotkeys to trigger DND in our macro I believe?

Is there anyone who has made/been using a macro with similar idea? Please share!~

Presuming you can write the macro for turning on / off the DND (I don’t off hand know how to turn DND on or off on the Mac, so I have no idea), the macro can be as simple as:

  • Turn DND on
  • Prompt For User Input “How many minutes for?” to variable Minutes
  • Pause Minutes * 60
  • Repeat 9999 Times
    • Prompt For User Input “How many more?” to variable Minutes (turn off Canceling of the Cancel button)
    • If Result Button is Cancel
      • Turn DND off
      • Cancel This Macro
    • Pause Minutes * 60

Something like that.

Thank you so much for your insightful reply. I will try and see what I can
get :slight_smile:

Sounds good - then you’ll have a framework in which to ask a more targeted question which is a good way to move forward.

In System Preferences > Keyboard > Shortcuts, there’s an item for Turn Do Not Disturb On/Off, and a system-wide shortcut can be added for this. Once a shortcut has been created, Keyboard Maestro can simulate this new shortcut using the Type A Keystroke action.

2 Likes

Hi Peter. I’ve tried to make one according to your formula as best as I can, but I think I still go something confused with some steps you listed that I cannot understand. After I click “try” the macro keeps running and the prompt dialogue box keeps hanging no matter what I did. I have to restart the computer to clear that. Would you show me a “realized” example of this macro so I can learn from your sample? Thank you.

p.s. I use “type keystroke” macro to trigger “DND” shortcut which I pre-set up in system preference.

Many thanks.

OK, which part are you having trouble with?

  • Can you write a macro that turns on/off DND?

The rest of the macro is something like:

Keyboard Maestro Actions.kmactions (2.9 KB)

1 Like

Thanks for your reply. I got stuck at the "If....then...." section but your illustration quickly clarified the puzzle and I can use it for my further study of using KM.

Here is my little "do not disturb" switch (with "JustFocus" and "Focus" bound together).

Switch- Toggle Do Not Disturb.kmmacros (8.1 KB)

The only remaining thing disturbing me is that I found an applescript online which can replace my current way of toggling DND (which is using its shortcut ctrl+=) for it can distinguish first whether DND is already on or not, which is more flexible. However, when I tried to run the script it tells me that --

Here is the script (turning DND on if it is currently not)

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

Thank you for your kindness and instruction. I'm pretty ignorant to KM and scripting as a seminary student but I'm trying to spend time on learning it.

That is a shell script, not an AppleScript.

The AppleScript part is the middle bit:

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

I just learned of an app called Undisturbed. It’s a menu bar app that toggles Do Not Disturb and has the added benefit of toggling Dock app icons (the red badges on app icons indicating new content). It requires 10.12, so I’m unable to test it or see if there’s a way to enable/disable the app capabilities with Keyboard Maestro. But it seems pertinent to this thread, so I’m passing it along.

http://app-enzeller.com/en/undisturbed