I would like to find an Applescript for turning on Do Not Disturb on my Mac. All I’ve found through googling is some very old scripts that don’t seem to work under Catalina. Does anyone know the current syntax?
I had the same problem. Therefore I included the System Shortcut in my AppleScripts:
When starting my AppleScripts a popup window appears in which I can enter the duration of the DND:
DND only:
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
end tell
set theTime to text returned of ¬
( display dialog (“How long should it remain activated?”) ¬
with title (“Do not Disturb”) ¬
default answer (“”) ¬
buttons {“Cancel”, “Start”} ¬
default button 2)
delay theTime * 60
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
delay 0.4
display notification “Is deactivated” with title “Do not Disturb” sound name “Tink”
end tell
DND & Sound:
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
set volume with output muted – mute volume
end tell
set theTime to text returned of ¬
( display dialog (“How long should it remain activated?”) ¬
with title (“Do not Disturb”) ¬
default answer (“”) ¬
buttons {“Cancel”, “Start”} ¬
default button 2)
delay theTime * 60
set volume without output muted – unmute volume
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
delay 0.4
display notification “Is deactivated” with title “Do not Disturb” sound name “Tink”
end tell
DND, Sound & Items:
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
set volume with output muted – mute volume
do shell script "defaults write com.apple.finder " & ¬
"CreateDesktop -bool false; " & ¬
“killall Finder” – hide desktop icons
end tell
set theTime to text returned of ¬
( display dialog (“How long should it remain activated?”) ¬
with title (“Do not Disturb”) ¬
default answer (“”) ¬
buttons {“Cancel”, “Start”} ¬
default button 2)
delay theTime * 60
tell application “System Events”
keystroke “d” using { command down , option down , control down , shift down }
set volume without output muted – unmute volume
do shell script "defaults write com.apple.finder " & ¬
"CreateDesktop -bool true; " & ¬
“killall Finder” – unhide desktop icons
delay 0.8
display notification “Is deactivated” with title “Do not Disturb” sound name “Tink”
end tell
Thanks, what I really wanted was for Keyboard Maestro to turn it on and off, but you reminded me that I could assign a keyboard shortcut in System Preferences and with that KM could easily toggle that on and off using a simple action. Thanks again.
Any reason you couldn’t run the Applescript in Keyboard Maestro?
That was what I originally intended to do, but since I can just use a simple keyboard shortcut in KM, I’ll just do that.
I am an Apple Script novice: this script does NOT work with my setup (Big Sur). I fail to identify which line corresponds to the action “enable Do Not Disturb mode”.
May I kindly ask you for help or further clarification?
@mgiugliano I use a different shortcut than in the AppleScripts above. DND works like this for me:
What AppleScript are you using that is not working?
Thank you for your reply and patience with me.
I have copy pasted the very first “DND only” script into the Script Editor (and replaced all double quotes with the correct " character). I was after a programmatic way to set DND from the command line rather than invoking a keyboard short-cut. Nonetheless, when I execute the script, the DND status does not change. Analysing the script, I could not find which is the line that “turn the switch” for DND.
I have nonetheless found an alternative approach. In my case, I intended primarily probing the DND status rather than setting it. I found that
defaults read com.apple.controlcenter "NSStatusItem Visible DoNotDisturb"
works. I
I found this application (Session @ stayinsession.com) is using something very similar, might be worth checking out there how to section on this and using their AppleScript even if you don’t use their app. I got it via my Setapp subscription.
Don’t be like me and skip their first step and then not understand why it won’t work
Works on Big Sir and Apple Silicon
Hi @mgguinne thanks for sharing the post
You helped me a lot with this.
I use the Timing App via my Setapp subscription for time tracking and the Hey Focus app for DND mode (also web pages).
Under macOS Catalina I had these apps combined in Keyboard Maestro.
Among other things with the possibility to hide files and folders and also to mute the system sound.
To be able to work really trouble-free, I sent a notification (Pushcut App) from the Keyboard Maestro macro to my iOS devices to activate/deactivate an individual DND mode via a Siri shortcut.
Here is a video about workflow under macOS Catalina
Thanks to you, I took the AppleScript from your linked post and finally got macOS Big Sur running.
Here is my Applescript for the workflow in the video:
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 "z" 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 "z" 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
What makes Pushcut very useful on iOS is the Automation Server, which allows me to have Siri perform shortcuts without picking up my iPhone.
Like in this example video for DND mode:
Given iOS 15’s new Focus mode that works across devices some of this may no longer be required?
As the OP, if the feature works as demonstrated, this should exactly what I want because it will allow Shortcuts to run when selected. I just hope it overcomes the split I’m experiencing in that my iPad for some reason never goes into DnD when I set it on my Watch or iPhone. I want to be able to set Focus mode once and have enabled everywhere.