Skip to content

Instantly share code, notes, and snippets.

@bezhermoso
Last active February 11, 2023 09:54
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bezhermoso/5f45ace434a7b03c963a to your computer and use it in GitHub Desktop.
Save bezhermoso/5f45ace434a7b03c963a to your computer and use it in GitHub Desktop.
Handly AppleScripts to manage OS X notifications. (Use in Automator, bind to keyboard shortcuts via BetterTouchTool, etc.) Inspired by http://apple.stackexchange.com/a/155736
# Close all notifications
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
try
click button 1 of this_item
on error
my closeNotifications()
end try
end repeat
end tell
end closeNotifications
# Dismiss only the top-most notification
my closeNotification()
on closeNotification()
tell application "System Events" to tell process "Notification Center"
click button 1 of last item of windows
end tell
end closeNotification
# Click top-most notification
my clickNotification()
on clickNotification()
tell application "System Events" to tell process "Notification Center"
click last item of windows
end tell
end clickNotification
@orenshk
Copy link

orenshk commented Apr 28, 2018

Awesome, thanks!

@johan456789
Copy link

Does clickNotification still work? I get the following error :(

System Events got an error: Can’t get last item of every window of process "Notification Center". Invalid index.

@janosmiko
Copy link

@johan456789 you should use "NotificationCenter" instead

@rviljoen
Copy link

You may need to update "Notification Center", depending on the language setting of your OS. In my case it is called "Notification Centre"

@JCsplash
Copy link

Is anyone able to dismiss banner notifications with this code? So far it only seems to dismiss alert notifications?

@sourcecodemage
Copy link

click button "Close" of window i

Stopped working , at least in Monterey.

tell application "System Events"
	tell process "NotificationCenter"
		set numwins to (count windows)
		repeat with i from numwins to 1 by -1
			perform (first action of group 1 of UI element 1 of scroll area 1 of window i where description is "Close")
			-- click button "Close" of window i
		end repeat
	end tell
end tell

I got that working, but it only closes one at a time, so something is wrong with my loop logic.

@blakegearin
Copy link

I believe this solves the looping issue:

tell application "System Events" to tell process "NotificationCenter"
	set numwins to (count windows)
	repeat with i from 1 to count of windows
		set number_of_notifications to count of groups of UI element 1 of scroll area 1 of window i
		repeat number_of_notifications times
			perform (first action of group j of UI element 1 of scroll area 1 of window i where description is "Close")
			delay 0.5
		end repeat
	end repeat
end tell

Without the delay there seems to be a race condition between the last close completing and the next close being triggered, which halts operation. Other related code I've found online use delay as well (1, 2). Though 0.5 is pretty conservative; you could probably get away with 0.2 on a new or fast machine.

cc @majorgear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment