rwxr--r--
/dev/blog

Bez Hermoso, Software Engineer @ ActiveLAMP

I think one glaring omission from OS X’s huge set of handy keyboard shortcuts for common functionality is the ability to dismiss all alert notifications using a global hotkey. Right now, the only way to clear out a backlog is to click on the “Close” button on each and every single notification item. Going through a backlog of alert notifications is a constant dilemma I face on a normal work day, and I was suprised when I learned there is no other way of dimissing alert notifications that is more efficient.

I actually found a solution by markhunte from Ask Different which worked quite well in the beginning. The solution in a nutshell is creating a custom service using Automator which executes a workflow that runs an AppleScript snippet that automates the dismissal of notifications, and assigning a global hotkey to execute it.

The solution works well but only when OS X cooperates – running the workflow within Automator cause zero problems, but making it work through a keyboard shortcut is hit-and-miss. I found myself mucking around with accessibility settings in System Preferences > Security & Privacy > Privacy a lot, adding system applications tucked away in /Library/CoreServices/ to the Accessibility list before getting it to work for the first time, until I discovered that the service simply fails with no explanation when invoked while I have certain applications in focus.

Eventually I figured out a more reliable way to accomplish this using a third-party application called BetterTouchTool.

I'll also share additional workflows I wrote that allows you to perform more fine-grained actions like acting on one notification item at a time, i.e. dismissing it, clicking it, or clicking on the secondary action if available. This is great for those alert notifications that supports quick reply:

Step 1: Create workflows in Automator

You will need to create a workflow in Automator for each of the AppleScript programs listed below.

On each workflow, select “Run AppleScript” from the Actions menu, put in the corresponding AppleScript code in the text box that appears on the right-side, and save the workflow somewhere you can find it later on.

1.1 Dismiss All Notifications.workflow

Actions > Run AppleScript…

on run {input, parameters}
    tell application "System Events" to tell process "Notification Center"
        click button 1 in every window
    end tell
    return input
end run

I must give credit to markhunte from the Ask Different as this accomplish almost exactly what his script does, albeit rewritten more concisely.

1.2 Dismiss Top-most Notification.workflow

Actions > Run AppleScript…

on run {input, parameters}
    tell application "System Events" to tell process "Notification Center"
        try
            click button 1 of last item of windows
        end try
    end tell
    return input
end run

1.3 Click Top-most Notification.workflow

Actions > Run AppleScript…

on run {input, parameters}
    tell application "System Events" to tell process "Notification Center"
        try
            click last item of windows
        end try
    end tell
    return input
end run

1.4 Click Secondary Action on Top-most Notification.workflow

Actions > Run AppleScript…

on run {input, parameters}
    tell application "System Events" to tell process "Notification Center"
        try
            click button 2 of last item of windows
        end try
    end tell
    return input
end run

Teaching AppleScript is obviously beyond the scope of this post. If you are not familiar with it, there are tons of articles online that can get you started, like this one.

Important: Do not save these in iCloud as you will need another non-MAS application to access them on a later step.

Step 2: Assign global hotkeys to the Automator workflows

There is a number of productivity applications out there that allows you to invoke an Automator workflow through a global hot-key, like Alfred and Keyboard Maestro.

However, I personally use BetterTouchTool as I already have it installed to accomplish similar tasks.

My mapping is as follows:

  • ⇧ ⌥ ⌘ ] - Dismiss all notifications
  • ⌥ ⌘ ] - Dismiss top-most notification
  • ⌥ ⌘ [ - Click top-most notification
  • ⌥ ⌘ ' - Click secondary action on top-most notification

BetterTouchTool is a really handy application all in all that will allow you to map an unthinkable amount of things to keyboard shortcuts, mouse & trackpad gestures, and a bunch of other peripherals. It has been a free application for a very long time, but it won't be long until the "pay as much as you want" licensing model will take into effect. But I highly recommend it.

Important: If you just installed BetterTouchTool, you will have to grant it access to accessibility services on your system. Go to System Preferences > Security & Privacy > Privacy, and add BetterTouchTool to the list under Accessibility.

Step 3

Try it out! Go ahead and prune thorugh your alert notifications with more effiency. Or, actually learn to turn on “Do Not Disturb” once in a while (no judgement here – I am also guilty of this.)

Extra keyboard-fu protip: You can quickly toggle “Do Not Disturb” by Option-clicking on the Notification Center icon on the menu bar, or you can actually assign a global hotkey to toggle it in System Preferences > Keyboard > Shortcuts Mission Control > Turn Do Not Disturb On/Off (It’s so simple I don’t even know why I don’t bother to do this.)

comments powered by Disqus