Dismiss MacOS Big Sur notifications with keyboard
Update
I'm posting a most robust version that evolved from my initial post. This seems to do a reasonable job of closing all windows, but it's sometimes slow to execute. As I've stated in the comments, I don't know much about AppleScript, so perhaps someone who knows what they're doing can tell us how to improve the performance.
activate application &quot;NotificationCenter&quot;
tell application &quot;System Events&quot;
    tell process &quot;Notification Center&quot;
        repeat
            
            try
                set theWindow to group 1 of UI element 1 of scroll area 1 of window &quot;Notification Center&quot;
            on error
                exit repeat
            end try
            
            try
                set theActions to actions of theWindow
                
                # Try to close the whole group first. If that fails, close individual windows.
                repeat with theAction in theActions
                    if description of theAction is &quot;Clear All&quot; then
                        set closed to true
                        tell theWindow
                            perform theAction
                        end tell
                        exit repeat
                    end if
                end repeat
                
                repeat with theAction in theActions
                    if description of theAction is &quot;Close&quot; then
                        set closed to true
                        tell theWindow
                            perform theAction
                        end tell
                        exit repeat
                    end if
                end repeat
                
            end try
        end repeat
    end tell
end tell

Original
Yes, thank you for posting the script, @AndrewJanian.
I received an error similar to @ColinFraizer's when using it as is. I think the issue is the inner repeat over the actions: it's trying to access actions in some cases after the window is closed. A minor tweak fixes this issue for me:
activate application &quot;NotificationCenter&quot;
tell application &quot;System Events&quot;
    tell process &quot;Notification Center&quot;
        set theWindow to group 1 of UI element 1 of scroll area 1 of window &quot;Notification Center&quot;
        # click theWindow
        set theActions to actions of theWindow
        repeat with theAction in theActions
            if description of theAction is &quot;Close&quot; then
                tell theWindow
                    perform theAction
                end tell
                exit repeat
            end if
        end repeat
    end tell
end tell