Shell Script – Do Not Disturb / If Then Action Works Irregularly

Hello KM Users,

i have found a shell script here in the forum, which tells me if the Do not Disturb mode is activated or not.

Unfortunately this did not work for me with the DNDStatus variable and also not with "contains 0". So I included it in an if Then action:

08)DND <EC1B 200722T010408>.kmmacros (23,8 KB)

08)DND <EC1B 200722T010408>

When I start the macro, it detects that DND is not active. But if I start the macro again nothing happens.
Only when I start the macro again, DND is deactivated.

2020_07_22_Support_1

Can anyone help me with this?

Hey @appleianer I think the problem is you are using "matches" instead of "contains". Normally "matches" is used for regex matching, but it would also work fine with 0 or 1 like you have been using it - IF that variable ONLY contains 0 or 1. If you check my original post you'll see it's set up to use "contains" because the result of the original command was a string like " doNotDisturb = 0;"

But, before you change it, you can actually change the command to the one below, then "matches" should work just as well. Or you can change it to "is".

I just updated the command to remove the need for grep, not sure if it will help you though. The command is just:

defaults -currentHost read com.apple.notificationcenterui doNotDisturb

Example usage:

I'm still on 10.14.6 though, so I'm not sure if this works on 10.15+, but it should be the same.

My Enable/Disable Macros are here.. Note that you need to change the Simulate Keystroke hotkey to match the key you've set in System Prefs.
DND.kmmacros (5.6 KB)

Thanks @jon123 for your answer and the modified macro.

I have installed your macro and unfortunately still had the error that the "then - action" could only be started when activating it for the second time.
The error was not in your macro but in the Focus app. I was able to fix this in the meantime, so that your macro, with my additions, now runs perfectly.

I use macOS Catalina 10.15.6

Thanks for your help :+1:

1 Like

Just updated to Catalina (fresh install though)... I'm actually having an issue with the hotkey on Catalina now. I've set the hotkey in System Prefs, but when I try to run the macro to execute the keystroke it doesn't work (error sound plays). Typing the shortcut myself works fine.

image

Hi @jon123, have you enabled all permissions for Keyboard Maestro in the security settings?

Yep. Just double checked, KM and KM Engine are both enabled for Accessibility.
KM enabled for "Full Disk Access".
Automation shows a few apps under KM, all enabled.
Tried turning them both off/back on in Accessibility, no still not working though.

Also my shortcut from Keyboard settings:

Did you include these programs with your new installation?

Please add KM also for input monitoring:

Thanks! I added those and also restarted, but it's still not working. When I manually run keystroke command via KM Editor I get the error sound:
image



Hi @jon123, this is always a sign that the keystroke is already in use, Have you tried another one?

I tend to agree, but if I press the shortcut with the keyboard it works fine - DND mode is enabled/disabled when I press it. Only get the alert sound when KM issues the keystroke.

I just tried changing the shortcut in system prefs, and making the same change in KM, still getting the error sound when trying to run it.

Couldn't get this resolved, so I switched to using this project: https://github.com/sindresorhus/do-not-disturb-cli which is working well.

Just had to set the KM ENV_PATH variable to "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" and now I can just run this to enable/disable DND:
image

2 Likes