3

This question offers an AppleScript to clear all notifications within the Notification Center, but all of the answers seem to be pre-Catalina:

How do I clear All OS X notifications with 1 click?

Post-Catalina, with the same script I get an error (reported via Keyboard Maestro) that the Notification Centre cannot access the "close" property of its windows. Looking through some of the AppleScript dictionaries it seems like Notification Center has no windows at all (anymore?).

What would be the way to accomplish this in Catalina?

2
  • The scripts in your referenced topic look like they are for the notification alerts. If you are talking about the main notification window, it only exists after you click the menu bar item. The UI elements for that appear to be the same as Mojave, but there isn't a "close" button per se - which one you are talking about?
    – red_menace
    Commented Nov 5, 2019 at 4:18
  • @red_menace Thank you for the clarification! I do mean the window (i.e. after you click the item in the menu bar). What should I be doing instead of the close command?
    – ksbrar
    Commented Nov 6, 2019 at 7:05

3 Answers 3

3

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
1
  • Thank you very much for the answer and explanation!
    – ksbrar
    Commented Nov 7, 2019 at 22:13
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
0

@red_menace has provided an excellent answer and explanation. I would choose to script the closing of all notifications by targeting all of them at once in order to perform a single action. It would be more efficient than a repeat loop, and should therefore produce a swifter clearance with little or no stalling.

use application id "com.apple.SystemEvents"

property SystemUIServer : a reference to process "SystemUIServer"
property menu bar : a reference to menu bar 1 of SystemUIServer
property icon : a reference to menu bar item "Notification Center" of my menu bar


-- If the notification centre icon isn't there,
-- terminate execution (result: -1)
if not (the icon exists) then return -1
click the icon -- Open notification centre

-- Clear all notifications
click button 1 of UI element 1 of (rows ¬
    of table 1 of scroll area 1 of ¬
    window "Notification Center" of ¬
    process "NotificationCenter" whose ¬
    class of button 1 of UI element 1 = button)

if not (the icon exists) return 0.5
click the icon -- Close notification centre
return 1

Note: Tested on High Sierra and Catalina with appropriate security privileges granted

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .