Clear Notifications in Big Sur

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;
}
3 Likes