4

In AppleScript, one can clear all notifications from all applications, like so:

tell application "System Events"
    tell process "NotificationCenter"
        set numwins to (count windows)
        repeat with i from numwins to 1 by -1
            click button "Close" of window i
        end repeat
    end tell
end tell

(Source)

Is it possible to clear the notifications from a specific application? For example, the script should remove the notifications from Calendar.app, but leave those that are from Google Chrome on the screen.


It may be necessary to explain what I intend exactly when I say, "clear." I am not trying to remove items from the list of notifications found in the "Notification Center" (i.e., the expandable vertical pane on the right side of the screen). I am trying to automate the action of pressing the "Close" buttons of the pop-up notifications that are superimposed in the top-right corner of the screen, so that these pop-up dialogs disappear from view.

This question concerns the "Alert" style of OS X notification (rather than the "Banner" style, which disappear on their own shortly after appearing).


OS X El Capitan, version 10.11.6.


2 Answers 2

2

I tested the following example AppleScript code under macOS 10.12.5, and it closed the Alerts style messages from Calendar.

tell application "System Events"
    tell process "NotificationCenter"
        set windowCount to count windows
        repeat with i from windowCount to 1 by -1
            if description of image 2 of window i is "Calendar" then
                click button "Close" of window i
            end if
        end repeat
    end tell
end tell

Note: This is example AppleScript code which contains no error handling. Add appropriate error handling as needed, required and or wanted.

3
  • I just tested the example AppleScript code under OS X 10.11.6, it closed the Alerts style messages from Calendar while leaving open others alerts from other apps. Commented Sep 29, 2017 at 6:59
  • 1
    Unfortunatley this no longer works under 12.5
    – spuder
    Commented Aug 4, 2022 at 14:45
  • I've noticed serveral variations of the process name, Notification Center, NotificationCentre, NotficationCenter, etc . Is there way to know which one is correct?
    – majorgear
    Commented Nov 14, 2022 at 17:19
-1

If you want to close only specific calendar alerts (maybe you used your calendar to schedule a daily process to run and it creates an alert that you don't want to have to deal with) you can use this code. Replace the "Put your alert text here" with whatever you used for the particular Calendar event name. It will cycle through all the notifications and only close the one you have identified if it exists.

tell application "System Preferences"
    tell application "System Events"
        tell process "NotificationCenter"
            repeat with i from 1 to count of windows
                if value of static text 1 of scroll area of window i contains "Put your alert text here" then
                    click button "Close" of window i
                end if
            end repeat
        end tell
    end tell
end tell

You must log in to answer this question.

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