How to clear all notifications with 1 click in Catalina?

Solution 1:

When scripting the user interface, for things like windows and menus the object hierarchy usually doesn't exist until it is shown, so the object needs to be shown before trying to get its properties. Once the window or menu is shown, you can use the Accessibility Inspector tool (included in the Xcode bundle) or manually spelunk the various UI elements to find the ones you are looking for. Interestingly, the Automator "Watch Me Do" action is also very handy for identifying specific UI elements (probably its best use), as the events it records can be copy/pasted into the Script Editor.

For the Notification Center window, the layout can vary, as individual notifications each have close/dismiss buttons, and there can also be an overall close/dismiss button for notifications grouped by application, although they aren't labeled as such. Since the topmost button is conveniently located in the same place, to just close/dismiss everything a script can be used to click on that element until there are no more (tested in Mojave and Catalina):

tell application "System Events"
    # show notification window
    tell application process "SystemUIServer" to click menu bar item "Notification Center" of menu bar 1

    # loop through the app close buttons
    tell process "Notification Center" to try
        repeat -- forever (at least until there are no more)
            delay 0.25
            click UI element 2 of UI element 1 of row 2 of table 1 of scroll area 1 of window "Notification Center" -- the topmost close button
        end repeat
    on error errmess -- no more
        log errmess
    end try

    # close the window
    # tell application process "SystemUIServer" to click menu bar item "Notification Center" of menu bar 1
end tell

Solution 2:

Building on top of @red_menace's answer, which only works if you happen to be on the "Notifications" Tab already. Here's the code for automatically switching to the "Notifications" tab (and switching back to the "Today" view if desired)

tell application "System Events"
    
    # Show "Notifications" Tab
    tell application process "SystemUIServer" to click menu bar item "Notification Center" of menu bar 1
    
    tell process "Notification Center" to click radio button 2 of radio group 1 of window 1
    
    # loop through the app close buttons
    tell process "Notification Center" to try
        repeat -- forever (at least until there are no more)
            delay 0.25
            click UI element 2 of UI element 1 of row 2 of table 1 of scroll area 1 of window "Notification Center" -- the topmost close button
        end repeat
    on error errmess -- no more
        log errmess
    end try
    
    # close the window
    tell application process "SystemUIServer" to click menu bar item "Notification Center" of menu bar 1
    
    # Return to "Today" Tab
    tell process "Notification Center" to click radio button 1 of radio group 1 of window 1
    
end tell