TIL: Toggle Do Not Disturb (With a Single Click)

During a brief discussion about PliimPRO, a menubar app to quickly set your Mac to a “safe” presentation mode, I learned that you can turn off desktop notifications by simply Option-clicking on the Notification Center icon on the menubar, without even having to open it first:

It's that simple. Just option-click, no need to open the menu first.

But that’s not all. Meandering even further along those lines, I realized that it would be trivial to build an Automator workflow to write a “presentation mode toggle” via AppleScript, and here it is for future reference:

if (do shell script "defaults read com.apple.finder CreateDesktop") is "1" then
    -- Get open apps
    tell application "System Events" to set openApps to name of every application process whose background only is false
    -- Do this for every open app
    repeat with processName in openApps
        tell application "System Events" to tell process processName
            set visible to false
        end tell
    end repeat
    -- Mute audio
    set volume with output muted
    -- Hide desktop icons
    do shell script "defaults write com.apple.finder CreateDesktop -bool false; killall -HUP Finder"
    -- Enable Do Not Disturb
    do shell script "defaults -currentHost write com.apple.notificationcenterui doNotDisturb -bool yes; defaults -currentHost write com.apple.notificationcenterui doNotDisturbDate -date \"`date -u +'%Y-%m-%d %H:%M:%S+0000'`\"; killall NotificationCenter"
else
    -- Unmute audio
    set volume without output muted
    -- Show desktop icons
    do shell script "defaults write com.apple.finder CreateDesktop -bool true; killall -HUP Finder"
    -- Disable Do Not Disturb
    do shell script "defaults -currentHost write com.apple.notificationcenterui doNotDisturb -bool no; killall NotificationCenter"
end if

And yes, this is a trifle gauche and overly liberal in its use of killall because it’s 2020 and Apple has let AppleScript degrade to such a point that most modern functionality is impossible to script directly without side effects (and also NotificationCenter seems to ignore SIGHUP, but that’s another matter entirely).

For good measure, you may want to add a “Change System Appearance” toggle after this script in your Automator workflow (you can pick Light, Dark, or Toggle Light/Dark):

This is a little hidden gem in Automator.

This obviously doesn’t switch wallpapers or do “smart” stuff like restoring hidden applications (I’d rather restore them myself, honestly), but is a pretty quick way to clear your desktop (and I used it to record the GIF above using Claquette, a great little utility I love and that I recently found via Michael Tsai).

Update: A little while after writing the above, I found the Pliim author has a public gist with a slightly different technique, and that the original app is on GitHub.