ccstone
September 11, 2021, 4:21am
21
martin:
This code works:
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
martin
September 11, 2021, 2:02pm
22
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
Video
3 Likes
hello
September 11, 2021, 9:06pm
24
appleianer:
HeyFocus
Is @appleianer HeyFocus better than ColdTurkey?
No @hello , the features are almost identical. However, ColdTurkey is cheaper
I use the HeyFocus app through my Setapp subscription. Therefore, the price is a minor issue for me.
martin
September 13, 2021, 12:54pm
27
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.
1 Like
Flix
September 13, 2021, 10:31pm
28
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
martin
September 13, 2021, 11:41pm
29
Hi Flix,
Flix:
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
This was suggested above by @Tom :
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.
In my macro above, I also use the hotkey to toggle DND state:
See my post above :
As for:
Flix:
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..
I think ==found image condition== should be avoided in general and is to be used as the last resort, because:
it consumes much resource when KM is looking for the image;
it makes the kmmacro file much larger (this can be avoided by using a image file instead);
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.
martin
September 17, 2021, 2:49am
30
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
1 Like
martin
October 16, 2021, 2:31pm
31
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.
2 Likes
I updated Martin Zhang's macro for Monterey!
The variable was changed so it made version 1 useless :))
Just change the app and it works perfectly for me.
DND v2 Monterey.kmmacros (10.1 KB)
You can also use the Shortcuts app to turn DND on or turn DND off via AppleScript.
tell application "Shortcuts Events"
run the shortcut "Turn DND on"
end tell
tell application "Shortcuts Events"
run the shortcut "Turn DND off"
end tell
2 Likes
larsof54 , Thanks, that's brilliant.