Turn Do Not Disturb on or off in an Applescript app (macOS Catalina) I want to turn on or off (not just toggle) Do Not Disturb within an Applescript-based app.
Apparently there used to be a method for doing this on older versions of the OS (I'm on Catalina). Now, the only solution I've found is a hack that uses a keyboard shortcut to toggle the condition of Do Not Disturb. This won't work for me because I don't know in advance the state of the setting before I run the app and I specifically want to turn it off when the app is run (later I'll turn it back on).
If there's no way to specifically turn Do Not Disturb on or off, then I suppose what I need to know is how to ascertain the current state of the setting and only execute the keyboard shortcut if it is not in the desired state.
This is not as elegant as I was hoping for but it seems to work.
After playing with the Record feature for a while, I seem to be able to turn DND on when it's off:
-- Click the “Notification Center” menu bar item.
delay 1.109957
set timeoutSeconds to 0.0
set uiScript to &quot;click menu bar item \&quot;Notification Center\&quot; of menu bar 1 of application process \&quot;SystemUIServer\&quot;&quot;
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “&lt;fill in title&gt;” button.
delay 2.096055
set timeoutSeconds to 0.0
set uiScript to &quot;click UI Element 3 of group \&quot;Do Not Disturb\&quot; of scroll area 1 of window \&quot;Notification Center\&quot; of application process \&quot;Notification Center\&quot;&quot;
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script &quot;tell application \&quot;System Events\&quot;
&quot; &amp; uiScript &amp; &quot;
end tell&quot;
            exit repeat
        on error errorMessage
            if ((current date) &gt; endDate) then
                error &quot;Can not &quot; &amp; uiScript
            end if
        end try
    end repeat
end doWithTimeout

and vice versa:
-- Click the “Notification Center” menu bar item.
delay 2.508349
set timeoutSeconds to 2.0
set uiScript to &quot;click menu bar item \&quot;Notification Center, Do Not Disturb enabled\&quot; of menu bar 1 of application process \&quot;SystemUIServer\&quot;&quot;
my doWithTimeout(uiScript, timeoutSeconds)

-- Click the “&lt;fill in title&gt;” button.
delay 1.176098
set timeoutSeconds to 2.0
set uiScript to &quot;click UI Element 3 of group \&quot;Do Not Disturb\&quot; of scroll area 1 of window \&quot;Notification Center\&quot; of application process \&quot;Notification Center\&quot;&quot;
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script &quot;tell application \&quot;System Events\&quot;
&quot; &amp; uiScript &amp; &quot;
end tell&quot;
            exit repeat
        on error errorMessage
            if ((current date) &gt; endDate) then
                error &quot;Can not &quot; &amp; uiScript
            end if
        end try
    end repeat
end doWithTimeout

It would be better if there were a solution that checked its state or explicitly specified the desired new state instead of just toggling. There is a small difference between the &quot;on&quot; and &quot;off&quot; scripts so it's not exactly toggling but it works by clicking the button.