Here is some code that works for me on MacOS MontereySonoma (14.2.1):
on closeNotifs(numTimes)
repeat numTimes times
try
tell application "System Events" to tell application process "NotificationCenter"
set boxes to groups of UI element 1 of scroll area 1 of window 1
set numBoxes to count boxes
repeat with i from numBoxes to 1 by -1
try
perform item 1 of (actions of item i of boxes whose name starts with "Name")
end try
end repeat
end tell
end try
end repeat
end closeNotifs
closeNotifs(2)
tell application "System Events" to tell application process "NotificationCenter"
try
log (actions of UI elements of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter" of application "System Events")
perform (actions of UI elements of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter" of application "System Events" whose name starts with "Name:Close")
end try
end tell
Instead of whose name starts with "Name""Name:Close"
you may use whose name description is "Close""Name:Fermer"
but I don't have English settings. I think this should work for anyin French, or your own language.
I haven't solved the problem that the last notification is not closed automatically, I added a log line so I basically run it twicethat you can check which action you wish to perform.
ThisAnyway, this has been a PITA for so long, hope this helps.
Check this for the Monterey version: https://apple.stackexchange.com/a/467607/303345