Last active
February 11, 2023 09:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
You may need to update "Notification Center", depending on the language setting of your OS. In my case it is called "Notification Centre"
Is anyone able to dismiss banner notifications with this code? So far it only seems to dismiss alert notifications?
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.
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
@johan456789 you should use "NotificationCenter" instead