How can I write a script that opens a notification?

Is there an apple script that can click on the notification for me so that I don't have to open the app using a mouse?

I plan to bind this script to a keyboard shortcut and that way I can open incoming notifications with just the keyboard.

Here are two notifications type examples

enter image description here

enter image description here

Note that scripts like this wont work because it attempts to click on a button and there's no consistent button to click on to open a notification. Sometimes its top, sometimes its bottom, sometimes its neither.

What is needed is a script that clicks on the "left" part of the notification, that will always open the app.


Solution 1:

Testing under macOS High Sierra, the following line of example AppleScript code, when run, had the following general effects on a notification:

tell application "System Events" to ¬
    click every window of application process "Notification Center"
  • Open the app for which the notification was from, if it wasn't already opened.
  • If the app was already open, it would set focus to the app.
  • If notification from multiple apps, the app on the top notification in the list gets opened/focus, additions apps get opened, if not already open.

Note that with some notifications, they will be dismissed; however, with others, the app will still be opened/focused as applicable but the notification will not be dismissed. These will need to be replied to appropriately.