Pragmatism in the real world

Keyboard control of macOS notification alerts

I use Apple’s Reminders app to remind me about things that I need to do. It has the useful feature of syncing across my Mac, iPad and iPhone and I can add to it via Siri in my car. When I am notified on my Mac, a notification alert appears with two buttons: Complete and Later. Clicking Later offers a set of options to dismiss the alert and have it reappear in 5 minutes, in an hour, tomorrow or to never appear again:

Later menu

It has been a source of frustration to me that I cannot access these buttons using my keyboard, so I decided to solve this problem using AppleScript and Alfred.

Clicking the Complete button

To click the Complete button, we can use the following AppleScript:

tell application "System Events"
	tell process "NotificationCenter"
		click button 1 of window 1
	end tell
end tell

In this code, we find the “Notification Center” process and click the first button of the first window. This will mark the reminder alert as complete. For other notifications, the first button is usually labelled Close, so we can close them too using the same script.

Clicking the Later button

The Later button too a bit more effort as its type is not button, but menu button. Once I discovered this, the same basic script works:

tell application "System Events"
    tell process "NotificationCenter"
        click menu button 1 of window 1
    end tell
end tell

This will open the sub menu attached to the Later menu button and, most importantly, put the keyboard focus on it, so that I can then use the arrow keys to select the option I want and press return to action it.

Run from the terminal

To run an AppleScript from the terminal, we can use /usr/bin/osascript, so I created two bash scripts, close-alerts and open-alert-menu with executable rights:

/usr/local/bin/close-alert:

#!/bin/bash
osascript -e 'tell application "System Events"
	tell process "NotificationCenter"
		click button 1 of window 1
	end tell
end tell'

(As you can imagine, /usr/local/bin/open-alert-menu is essentially the same but clicks menu button 1 instead.)

Mapping to Alfred commands

For easy access to these scripts, I use custom Alfred workflows. There are easy to set up. On the Workflows tab

  • Create a new Keyword Input and set:
    1. Keyword to open alert menu with No Argument required
    2. Title to Open alert menu
  • Create a new Run Script Action and set the Script to /usr/local/bin/open-alert-menu

Repeat for close alert, and you end up with:

Alfred workflow

That’s it

I can now trigger Alfred and type open alert menu to enable me to control when I want to be re-reminded about an alert.

Alfred open alert menu

AppleScript is arguably my least favourite programming language as it tries to look like English and feels about as ambiguous. However, having a system-wide scripting system is quite useful!

7 thoughts on “Keyboard control of macOS notification alerts

  1. You might want to omit the bash scripts/files and then use a Alfred action – Run NSAppleScript.

    1. I like to use bash scripts as I can call them from the terminal or Keyboard Maestro too. Keeps it all consistent for me.

  2. Thank you! I have been so confused about how to click the "Actions" button in Notification Center alerts. menu button works!

  3. Exactly the solution I wanted to code myself – thanks for writing it, much appreciated!

  4. Just figured out, that if you want to click on a "Reply" message, you need the following:
    tell application "System Events"
    tell process "NotificationCenter"
    click button 2 of window 1
    end tell
    end tell

    Thanks again for this great article!

Comments are closed.