Turn Do Not Disturb on or off in an Applescript app (macOS Catalina)
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.