Developing software in the Real World

Keyboard control of Big Sur notification alerts

With the release of Big Sur, my scripts for keyboard control of macOS notification alerts stopped working.

With a bit of experimentation and help from Google, I have written new ones.

Closing an alert

To close a notification alert on Big Sur, conceptually we want to press the X button that appears when you hover over it. The AppleScript to do this is:

activate application "NotificationCenter"
tell application "System Events"
    tell process "NotificationCenter"
        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

This is much more complicated than just clicking button 1 of window 1 which is what we did before! In Big Sur, NotificationCenter now has a set of actions within the window and we iterate over them looking for the one called “Close” and then execute it.

This closes the notification alert.

Running from the command line

To run it from the command line, I have a file called close-alert stored in /usr/local/bin that runs `osascript` for me:

/usr/local/bin/close-alert:

#!/bin/bash
osascript -e 'activate application "NotificationCenter"
tell application "System Events"
    tell process "NotificationCenter"
        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'

This is marked as executable (chmod a+x /usr/local/bin/close-alert) so that I can close a notification alert from Terminal

Running from Alfred

I have also added to Alfred as a workflow. This requires two steps: a keyword step called “close alert” which takes no argument that is linked to a run script step which runs /usr/local/bin/close-alert.


Alfred Close Alert Workflow

This allows me to type “close alert” into Alfred to close the notification.


Alfred Close Alert

This gives me back keyboard control of closing the Big Sur notifcation alerts which is the operation I do most.

One thought on “Keyboard control of Big Sur notification alerts

  1. Hi,

    This doesn't seem to work. I could not find a window named "Notification Center". I'm running Monterey btw.

    Exact error:

    error "System Events got an error: Can’t get window \"Notification Center\" of process \"NotificationCenter\"." number -1728 from window "Notification Center" of process "NotificationCenter"

    Any idea why I'm getting this?

    Thanks in advance!

Thoughts? Leave a reply

Your email address will not be published.