Scripting Notification Alerts to Notification Center in Mountain Lion

The terminal-notifier command is a tool used for sending messages and actions to the Notification Center. It’s a gem, so to set it up we’ll first run the gem command to install it by name:

gem install terminal-notifier

Once installed, run the command along with the -message option followed by a quoted message:

terminal-notifier -message "Hello world"

This produces a message from Notification Center as follows:

terminalnotifier1

The title on the screen though, says Terminal. We want to change the title to something else. To do so, add the -title option. Adding the -title option along with a quoted title then displays a title in the top of the notification.

terminalnotifier2

You can also add a subtitle, which allows for you to, for example, add the name of the app in either of the two fields along with the action you’re performing in the other. For example, if you wanted to add Notification Center alerts to TripWire you could do something like this:

terminal-notifier -message "There were no updates today" -title "TripWire" -subtitle "Drive Scanned"

I like to add sound to some, done using the cleverly named -sound option. Sounds include: blow, bottle, frog, funk, glass, hero, morse, ping, pop, purr, sosumi, submarine and tink. In the following example, I’m going to go ahead and do pretty much the same thing but have a command expand into the message and have it make the pop sound:

terminal-notifier -message `cat mylog.file` -title "TripWire" -subtitle "Drive Scanned" -sound pop

You can add a -group number, which is like a process ID for notifications:

terminal-notifier -message `cat mylog.file` -title "TripWire" -subtitle "Drive Scanned" -sound pop -group 101

You can then list the jobs that you fired off by the -group ID supplied earlier. For example, the following command:

terminal-notifier -list 101

Shows output as follows:

GroupID Title Subtitle Message Delivered At
101 TripWire Drive Scanned There were no updates today 2013-09-22 20:19:53 +0000

You can then remove a job by the same ID using the -remove option:

terminal-notifier -remove 101

You can also invoke actions using the -open and -execute options as well as the -activate option. Open fires up a web page, execute runs a shell script and activate opens an application. We’ll start with the script by adding -execute and linking to a shell script, that will run when someone clicks on the notification dialog:

terminal-notifier -message `cat mylog.file` -title "TripWire" -subtitle "Drive Scanned" -sound pop -group 101 -execute /scripts/champagne.sh

The -open instead of execute will fire off a web page:

terminal-notifier -message `cat mylog.file` -title "TripWire" -subtitle "Drive Scanned" -sound pop -group 101 -open http://www.krypted.com

Or use -activate to fire up an app, following it with the bundle ID:

terminal-notifier -message `cat mylog.file` -title "TripWire" -subtitle "Drive Scanned" -sound pop -group 101 -activate com.microsoft.word

Thanks to Peter for bringing this tool to my attention. I haven’t figured out how to make it persistent yet, working on that.