Close/Open all Notification Centre Alerts and Banners with a Keyboard Shortcut

[wce_code id=”10″]

Screen Shot 2014-11-13 at 21.23.22

If there are times when you want to close or open in one fell swoop, all the on screen Alerts and banners that Notification centre has populated your screen with. But could not find a way.

One way you can  is by creating an Automator service to run the below  Applescripts and give them a keyboard shortcut in the System Preferences Keyboard shortcuts to do so.

Here is how.


In Automator choose to create a new service

enter image description here


Add a Run Applescript Action

enter image description here


Replace it’s standard 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  Service receives [no input] in [any application]


 

Save the service


Open the Keyboard shortcuts in System prefs and set your shortcut for your service under ‘Services’

enter image description here

Now any newly launched app will pick the keyboard shortcut up.


 

[wce_code id=”3″]

 I structured the script to counter throwing an error that will occur when the notifications/windows start to close.The Notifications/windows are numbered 1 through to the total count. But as they close the script would normally still be working of the old count.But the system will be re assigning the index of the windows to the remaining ones.So where we  start for example 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 index numbers 1,2,3 to the last remaining windows 4,5,6. Windows 4,5,6 no longer exist only 1,2,3 now exist. But the script will try and close a window with the index 4 and throw an error because it does not exist. The script structured to 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

                                                        my closeNotif()

                                              end try

                           end repeat

            end tell

end tell

end closeNotif

Hi, Please Leave a Reply