I don't see a question, but I assume you're wondering why it doesn't work.
Try this instead:
tell application "System Events"
try
set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
repeat with _group in _groups
set _actions to actions of _group
repeat with _action in _actions
if description of _action is in {"Close", "Clear All"} then
perform _action
end if
end repeat
end repeat
end try
end tell
Thank you, your suggestion is a good way to repeat over a list. The original solution works but checks for "Clear All" first before moving on to check for "Close". With the list you suggested, for some reason a group of alerts that is expanded, this will close all but the last alert. ¯_(ツ)_/¯
Yours does work better! The reason it didn't work for me was the end tel
typo, which I missed at first. Once that was fixed, it works great and, as you mentioned, doesn't leave one notification at the end.
Ahh... I missed that too. Must have hit the delete key right before I shared this. I updated the macro and the jpeg to show the missing "L".
For anyone who doesn't know what is going on (because its since been updated), the original macro was missing 1 "L" in the last "tell" of the script, so it was actually "tel" ... which will break.
Thanks for the good catch @noisneil
Thanks for that macro. Clearing notifications was going to be my next project. Now I'll have to think of something else to do.
Emyr
My pleasure. I'm sure this macro can be tweak to perform better. I'd love to see your updates if you work on it.
hello @noisneil
great macro. Clearing notifications is always a big pain in the neck. Thank you very much. The problem is that when there are many notifications, I have to run the macro multiple times. This morning for example with 10+ notifications when I opened my mac, I had to run the script 3 times.
Should I just sequentially run the macro multiple times with the repeat action or would you have a solution which you would integrate into the script itself.
thanks very much
Here's a new version that should be more robust:
tell application "System Events"
repeat
-- Assume no notifications by default
set hasNotifications to false
try
-- Get the list of notification groups
set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
-- If we find at least one notification group, set the flag to true
if (count of _groups) > 0 then
set hasNotifications to true
end if
-- Iterate over the notification groups and clear them
repeat with _group in _groups
try
-- Try to perform the "Clear All" action if available
perform (first action of _group where description is "Clear All")
on error
-- If "Clear All" isn't available, try the "Close" action
perform (first action of _group where description is "Close")
end try
end repeat
-- Add a brief delay to allow Notification Center to update
delay 2
on error errorMessage
-- Handle any errors that might occur and log them
log "Error encountered: " & errorMessage
end try
-- Exit the loop if no notifications were found
if not hasNotifications then exit repeat
-- Additional delay for rechecking notifications
delay 2
end repeat
end tell
Nothing happens. Engine error message below.
thank you for taking the time to rewrite the script.
2023-11-13 15:38:37 Execute macro “Clear all Notifications” from trigger Editor
2023-11-13 15:38:37 Action 15233024 failed: Execute an AppleScript failed with script error: text-script:1192:1192: script error: Expected end of line, etc. but found end of script. (-2741)
2023-11-13 15:38:37 Execute an AppleScript failed with script error: text-script:1192:1192: script error: Expected end of line, etc. but found end of script. (-2741). Macro “Clear all Notifications” cancelled (while executing Execute AppleScript).
Did you incorporate it into a macro or try to run it alone? That may be the problem. This works nicely for me on Ventura:
Clear All Notifications.kmmacros (48 KB)
Now it's fine. Thanks very much !!
Hi ! I use the clear all notifications macro many times a day.
One problem I have is that many notifications are of little interest and I want to clear them regularly, but I want to keep the notifications emanating from the Reminders app. Is there any way to create a variant that clears all notifications except those coming from the reminders app ?
thank you very much
Not thoroughly tested, but give this a try:
tell application "System Events"
repeat
set hasNonReminderNotifications to false
try
-- Get the list of notification groups
set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
-- Iterate over the notification groups and clear them, except for Reminders
repeat with _group in _groups
-- Get the name of the group (app name)
set groupName to name of first UI element of _group
-- Only clear if it's not from Reminders
if groupName is not "Reminders" then
set hasNonReminderNotifications to true
try
-- Try to perform the "Clear All" action if available
perform (first action of _group where description is "Clear All")
on error
-- If "Clear All" isn't available, try to close individual notifications
set notifications to UI elements of _group
repeat with notification in notifications
try
perform (first action of notification where description is "Close")
end try
end repeat
end try
end if
end repeat
-- Add a brief delay to allow Notification Center to update
delay 2
on error errorMessage
-- Handle any errors that might occur and log them
log "Error encountered: " & errorMessage
end try
-- Exit the loop if no non-Reminder notifications were found
if not hasNonReminderNotifications then
log "All non-Reminder notifications cleared. Exiting."
exit repeat
end if
-- Additional delay for rechecking notifications
delay 2
end repeat
end tell
thanks very much.
error message
2024-08-09 19:51:33 Execute macro “Clear all Notifications EXCEPT REMINDERS” from trigger The Hot Key ⌃⌥⇧⌘F7 is pressed
2024-08-09 19:51:33 Action 16023758 failed: Execute an AppleScript failed with script error: text-script:1335:1335: script error: Expected end of line, etc. but found end of script. (-2741)
Strange. Works fine here.
Here's the whole macro, just in case that helps:
Clear All Notifications (excluding Reminders).kmmacros (42 KB)
great ! thanks very much