Dismiss MacOS Big Sur notifications with keyboard

Solution 1:

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 "NotificationCenter"
tell application "System Events"
    tell process "Notification Center"
        repeat
            
            try
                set theWindow to group 1 of UI element 1 of scroll area 1 of window "Notification Center"
            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 "Clear All" 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 "Close" 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 "NotificationCenter"
tell application "System Events"
    tell process "Notification Center"
        set theWindow to group 1 of UI element 1 of scroll area 1 of window "Notification Center"
        # click theWindow
        set theActions to actions of theWindow
        repeat with theAction in theActions
            if description of theAction is "Close" then
                tell theWindow
                    perform theAction
                end tell
                exit repeat
            end if
        end repeat
    end tell
end tell

Solution 2:

I have an AppleScript that works. I found the elements using UIBrowser. Caveat is that the notification must have a "Close" action. All of the notifications I have encountered have that action.

activate application "NotificationCenter"
tell application "System Events"
    tell process "Notification Center"
        set theWindow to group 1 of UI element 1 of scroll area 1 of window "Notification Center"
        click theWindow
        set theActions to actions of theWindow
        repeat with theAction in theActions
            if description of theAction is "Close" then
                tell theWindow
                    perform theAction
                end tell
            end if
        end repeat
    end tell
end tell