Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created July 18, 2017 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathggns/faf8f3bca45d11f4d08e2109e75bf67a to your computer and use it in GitHub Desktop.
Save nathggns/faf8f3bca45d11f4d08e2109e75bf67a to your computer and use it in GitHub Desktop.
Applescript (JXA) code to get a list of notifications
#!/usr/bin/env osascript -l JavaScript
const NC_HIDDEN_POSITION = [99999, 99999];
class NotificationCenterBridge {
constructor() {
this.systemEvents = Application('System Events');
this.uiServer = this.systemEvents.applicationProcesses.byName('SystemUIServer');
this.notificationCentre = this.systemEvents.processes.byName('Notification Center');
this.notificationTable = this.notificationCentre.windows.byName('NotificationTableWindow');
}
toggle() {
this.uiServer.menuBars()[0].menuBarItems()[0].click();
return this;
}
readNotificationsFromOpen() {
const rows = ObjC.unwrap(this.notificationTable.scrollAreas[0].tables[0].rows());
return rows.slice(2).map(notification => {
try {
const texts = notification.uiElements[0].staticTexts();
const [app, when, title, _, value] = [0, 1, 2, 3, 4].map(i => texts[i].value());
return { app, when, title, value };
} catch (e) {
return null;
}
}).filter(n => n);
return notifications;
}
hide() {
this.notificationCentre.windows[0].position = NC_HIDDEN_POSITION;
return this;
}
}
function run() {
const nc = new NotificationCenterBridge();
const notifications = nc
.toggle()
.hide()
.readNotificationsFromOpen()
nc.toggle();
return notifications;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment