Found a great script for dismissing all notifications on MacOS

I still need persistent notifications on my MacBook Pro, but boy is it annoying when I open my mac after 12+ hours, and I’m greeted to a wall on the right side of my screen of over 20 notifications. And in 2018, it’s crazy that there is still not a “Dismiss All Notifications” command built into the OS. So, click, click, click, click, seemingly forever.

I finally found a great applescript to do the trick. Using Better Touch Tool, I’ve mapped it to Alt-Cmd D, and also added it as a button to my Touch Bar. I highly, highly recommend it. I wish I could take credit for writing the script. I don’t even know if it’s optimized for this use, but it’s been great:

my closeNotifications()
on closeNotifications()
	tell application "System Events" to 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
				click button 1 of this_item
			on error
				my closeNotifications()
			end try
		end repeat
	end tell
end closeNotifications

I could use something like this, but the script does not seem to work for me…

  • If I run it with Notification Center open: nothing happens.
  • If I run it with Notification Center closed: the script will open the Notifications tab of System Preferences

What am I doing wrong?

Very cool! Thank you.

Instead of adding it as an actual button to BTT, I added it as an AppleScript widget to make the button appear only when there are notifications - otherwise it is hidden to save space. Like this:

tell application "System Events"
	if exists (window 1 of process "Notification Center") then return "Dismiss All"
end tell
return "" 

I also have it set to only show the icon, which hides the “Dismiss All”. BTT is crazy powerful, particularly when it comes to the touchbar.

@rob, it worked like a charm for me as-is, using copy/paste to avoid typos. If you run it with Script Editor, what result does it show?

I just tried running it in Script Editor with similar results to @rob. In my case, nothing happens with the notifications showing or not showing. Oddly, a very short click on the “run” button in the Script Editor when the window is showing shows this result:

button 1 of window “Notification Center” of application process “NotificationCenter” of application “System Events”

To clarify, the script dismisses Notifications (separate windows with two buttons each), NOT the items listed in Notification Center. In testing, if I open Notification Center without any Notifications present and fire the script before it auto-closes, it will launch Notification Center settings. If Notifications are present, it works as expected.

1 Like

Bingo! I was expecting the latter…

(I was already surprised that every notification in there would be a separate window)

Same here; I misinterpreted. I never get more than a few notifications, and most fade away in just a few seconds, so this might not be all that useful to me.

I like the TouchBar part :slight_smile: I did try to find a way to clear the Notification Center with AppleScript, but no luck.

sorry I wasn’t more clear! Thanks, evan for clarifying!!!

Okay, now you got me. So, what did you do? I’m guessing under BTT-TouchBar, you added a Widget.
“Run Apple Script and Show Return Value?” is the apple script above separate from the script that I posted? (Is the widget referring to a separate script?) Or, is it all one long apple script, with your code at the top?

Yep, exactly. That’s where you put the code I showed above. Then the predefined action is “Run Apple Script” with your code.

Evan, I finally have a minute to get back to this. It took me a minute to figure out how to set it up, but I finally got it. the Applescript of the widget is returning whether the button exists or not, and then i’m putting the applescript to execute the dismiss all under the Predefined Action if that button is touched. Very clever. THANKS!

1 Like

Right on! I could have been clearer in my explanation, but that is a useful trick for keeping the touchbar from cluttering up.