AppleScript Do Not Disturb on Mac?

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.

1 Like

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.

1 Like