This macro is similar to others for clearing out notifications in Big Sur. Right now, it only supports a single group. I haven't had multiple groups of notifications to test yet.
If anyone has figured out a cleaner way to do this, I would love to hear it. I couldn't find a clean way to move the mouse or get access to the hidden "X" and "Clear All" buttons without having the mouse hover over the notification in Big Sur.
Really like M1 Macs, really not liking the mess that is the new notifications.
So if I highlight a notification so that the X appears and run this applescript
my closeNotifications()
on closeNotifications()
tell application "System Events" to tell process "NotificationCenter"
click button 2 of group 1 of UI element of scroll area 1 of window 1
end tell
end closeNotifications
It should close, right? But does not close at all for me.
I keep everything unstacked because it's easier to manage that way. Hoping to find way to just nuke all notifications after I've looked through them. I keep them on screen permanently as there are important things I will not notice otherwise, and do this on my main iMac and M1 Macbook Air, so I have them in two places. Really want to my "clear all notifications" solved.
It is slower than macOS Catalina... but it is better than nothing.
If you use an english macOS, then you have to change "NotificationCenter" to "Notification Center" in the script.
I may have discovered a way to clear the notifications without the need to move the mouse to show the Clear All button. My tests so far of this script have been successful without the need to move the mouse.
var app = Application("System Events");
var notificationCenter = app.processes.byName("Notification Center");
var group = notificationCenter.windows
.at(0)
.scrollAreas.at(0)
.uiElements.at(0)
.groups.at(0);
var actions = group.actions.whose({
_or: [{ description: "Clear All" }, { description: "Close" }],
});
try {
var uniqueNames = new Set();
var actionsToPerform = [];
actions().forEach((a) => {
if (!uniqueNames.has(a.name())) {
actionsToPerform.push(a);
}
uniqueNames.add(a.name());
});
var uniqueDescriptions = new Set(
actionsToPerform.map((a) => a.description())
);
if (uniqueDescriptions.has("Clear All")) {
var clearAll = actionsToPerform.filter(
(a) => a.description() === "Clear All"
)[0];
clearAll.perform();
} else {
actionsToPerform.forEach((a) => a.perform());
}
} catch (e) {
console.log(e.message);
throw e;
}
This worked great for me using "Execute JavaScript For Automation" as the action. I removed the last line throw e; so if the action was fired without any notifications present the code completes instead of erroring.
The following script allows, without moving the mouse, to:
Close single notification.
Show more notifications.
Clear all notifications.
tell application "System Events"
tell process "Notification Centre"
if button "Confirm Show Less" of UI element 1 of scroll area 1 of window "Notification Center" exists then
click button "Clear" of UI element 1 of scroll area 1 of window "Notification Center"
delay 0.25
click button "Clear" of UI element 1 of scroll area 1 of window "Notification Center"
else
set static_text_nr to 3
set static_text_list to {}
set count_static_text to count static text of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
set count_static_text to count_static_text - 2
repeat with x from 1 to count_static_text
set value_of to get value of static text static_text_nr of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
set end of static_text_list to value_of
if static_text_nr = (count static text of group 1 of UI element 1 of scroll area 1 of window "Notification Center") then exit repeat
set static_text_nr to static_text_nr + 1
end repeat
log static_text_list
set list_item to 1
if last item of static_text_list contains "more notification" then
click group 1 of UI element 1 of scroll area 1 of window "Notification Center" -- Show more notifications
else
perform the last action of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
end if
end if
end tell
end tell
--if testvar contains "more notification" then return "YES"