Skip to content

Instantly share code, notes, and snippets.

@aidilfbk
Created March 22, 2018 10:50
Show Gist options
  • Save aidilfbk/96ae970a88717552e2a10ecfd0a3b259 to your computer and use it in GitHub Desktop.
Save aidilfbk/96ae970a88717552e2a10ecfd0a3b259 to your computer and use it in GitHub Desktop.
Dump this into Script Editor.app and run when you want to clear all notifications in the Notifications Center panel
var app = Application("System Events");
var ncToggle = app.processes.byName('SystemUIServer').menuBars[0].menuBarItems.byName("Notification Center");
ncToggle.click(); // show Notification Center panel on screen right
var notificationCenter = app.processes.byName('NotificationCenter');
var ncWindow = notificationCenter.windows[0];
var ncNotifTabBtn = ncWindow.radioGroups[0].radioButtons.byName("Notifications");
var originalNotifTab = "notifications";
if(!ncNotifTabBtn.value()){
ncNotifTabBtn.click(); // switch to "Notifications" tab if currently on "Today" tab
originalNotifTab = "today";
}
var ncTable = ncWindow.scrollAreas[0].tables[0];
if(ncTable.rows().length > 1){ // ignore the "Do Not Disturb" hidden row
var numNotificationGroups = ncTable.rows().map(function(row){
return row.uiElements[0].buttons();
}).reduce(function(allButtons, btnArray){
return allButtons.concat(btnArray);
}, []).filter(function(button){
return button.description() == "clear";
}).length;
while(numNotificationGroups--){
// doing this way instead of using Array.map above because the selector dynamically refreshes in the background instead of binding to the same object
ncTable.rows[1].uiElements[0].buttons[0].click();
}
}
if(originalNotifTab === "today"){
ncWindow.radioGroups[0].radioButtons.byName("Today").click(); // restore to initial "Today" tab
}
ncToggle.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment