26

I'd like to be able to dismiss and open notifications with the keyboard

(Not talking about opening/closing the notification center)

Is this possible? And if so, how?

  • Did you see that update you asked for? – markhunte Nov 12 '14 at 9:30
25

You can create Automator service to run this Applescript and give it a keyboard shortcut in the System Preferences Keyboard shortcuts

This will close Alert and banners Notification


In Automator choose a new service

enter image description here


Add a Run Applescript Action

enter image description here


and replace it's code with:

my closeNotif()
on closeNotif()

    tell application "System Events"
        tell process "Notification Center"
            set theWindows to every window
            repeat with i from 1 to number of items in theWindows
                set this_item to item i of theWindows
                try
                    click button 1 of this_item
                on error

                    my closeNotif()
                end try
            end repeat
        end tell
    end tell

end closeNotif

Set the 'Service receives [no input] in [any application]'

Save the service.


Open the Keyboard shortcuts in System prefs and set your for your service under 'Services' enter image description here

Now any newly launched app will pick the shortcut up.

(Note: I structured the script to counter throwing an error that will occur when the notifications/windows start to close.

otifications/window are numbered 1 through to the total count. But as they close the script would still be working of the old count. But the system will be re assigning the index of the windows.

So where we say start at 1 -6 the script will try and close window 1, window 2, window 3 and so on. But the system has re assigned the window numbers 1,2,3 to the last remaining windows. But the script will try and close window 4 and throw an error because it does not exist. The script will catch this and deal with it. )


If you want to click the 'Show' button on an Alert Notification. you change the button you click from 1 to 2.

click button 2 of this_item

Banner notifications do not have a button 2.

But you can just click the window.

So this code should take care of Showing.

my closeNotif()
on closeNotif()

    tell application "System Events"
        tell process "Notification Center"
            set theWindows to every window
            repeat with i from 1 to number of items in theWindows
                set this_item to item i of theWindows
                set cnt to count buttons of this_item
                try
                    if cnt > 1 then

                        click button 2 of this_item
                    else
                        click this_item
                    end if
                on error

                    closeNotif()
                end try
            end repeat
        end tell
    end tell

end closeNotif
  • Thanks, this is really cool. The one thing it's missing is how to also open a notification rather than close it... is that something you could add, and I will gladly accept your answer! – Drewdavid Nov 11 '14 at 0:15
  • @Drewdavid updated the answer :-) – markhunte Nov 11 '14 at 1:33
  • 1
    Drewdavid: I am having the same problem, and gave up fixing it under Security & Privacy and just set up a global hot-key through BetterTouchTool. – Bez Hermoso Jan 17 '16 at 8:26
  • 1
    Note: the process "Notification Center" can be named either "Notification Center" of "NotificationCenter". The System will understand both within Applescript. It is normal to use the app name in AppleScript. In this case Notification Center". If you are using a do shell script or unix then use the actual process name "NotificationCenter" – markhunte Sep 10 '17 at 22:31
  • 1
    @Chris, save the workflow as an Application in Automator. Named something like "Close All Noifications.app" Then add it to the Accessibility manually. Then create a service as described above but in the run Applescript use : "tell application "Close All Noifications" to activate" – markhunte Jun 20 '18 at 18:27
6

Not quite what you asking for:

You could limit the time displayed for banners type with

Terminal and paste in the following

defaults write com.apple.notificationcenterui bannerTime #

with the # number sign replaced with the amount of seconds you want banner notifications to stick around, then log off and back on.

To restore original function (5 seconds) use defaults delete com.apple.notificationcenterui bannerTime

I know you said no: but you could cycle the Notification on/off with a script and assign a keyboard short cut to it. Temporarily disable Notification Center in Mountain Lion from command line?

  • Thanks for posting; the time limit is also worth noting here :) – Drewdavid Nov 12 '14 at 21:43
  • Does this work in High Sierra? I couldn't get it to have any effect. – luckman212 Sep 17 '18 at 14:57
0

The original script by markhunte works but stops after a few windows. It may be that the list of windows only includes the ones that are currently visible. When you have too many this won't close all. I added a loop outside the main loop to query the windows until we get a window count of zero. Here is the code:

my closeNotif() on closeNotif()

tell application "System Events"
    tell process "Notification Center"
        set theWindows to every window
        set nWindows to number of items in theWindows
        repeat until nWindows is 0
            repeat with i from 1 to number of items in theWindows
                set this_item to item i of theWindows
                try
                    click button 1 of this_item
                    delay 0.2
                on error

                    my closeNotif()
                end try
            end repeat
            set theWindows to every window
            set nWindows to number of items in theWindows
        end repeat
    end tell
end tell

end closeNotif

You must log in to answer this question.

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