Send an Alert to Notification Center from the Command Line in OS X
Using an excellent third party tool called terminal-notifier, you can post alerts and messages to Notification Center directly from the command line. This has a myriad of potentially valid uses, but one fantastic use-case is along the same veins of verbally announcing when a command has completed or sending a badge alert, but instead posting the notification to OS X Mountain Lion’s Notification Center.
Installing Terminal Notifier
Assuming you have ruby on the Mac, you can easily install terminal-notifier using gem:
sudo gem install terminal-notifier
For those without ruby, you can download a pre-built binary from GitHub but to run terminal-notifier you have to point it to the binary inside the app bundle as so:
If you go the latter route, you’d best off creating an alias in bash_profile. For the purpose of this article we’ll assume you installed it through ruby.
Using Terminal Notifier to Post to Notification Center
Once installed, using the command at it’s most basic core is as follows:
terminal-notifier -message "Hello, this is my message" -title "Message Title"
Posting a message after a command has completed is easy, just append terminal-notifier as so:
ping -c 5 yahoo.com && terminal-notifier -message "Finished pinging yahoo" -title "ping"
These post a noninteractive notification, but digging deeper you can launch applications, execute terminal commands, and open URLs too.
Making Notifications Interactive: Opening URL’s, Applications, and Executing Terminal Commands
Even better though are the -open and -activate commands though, which let you either specify a URL or an application to activate when the Notification is clicked. For example, this will open osxdaily.com when clicked:
terminal-notifier -message "Go to OSXDaily.com, it's the best website ever!" -title "osxdaily.com" -open https://osxdaily.com
The notification posts to Notification Center, and if clicked it will open osxdaily.com in the default web browser.
The next example will open TextEdit if you click on the notification:
terminal-notifier -message "Time to braindump into TextEdit" -title "Braindump" -activate com.apple.TextEdit
You can also execute terminal commands if the notification is interacted with:
terminal-notifier -message "Time to run your backups" -title "Backup Script" -execute backupscript
That’s just a few examples, but there are obviously infinite uses for such a thing. Considering how useful this is I’m surprised Apple didn’t include a way to do this into OS X, though that could change some day. In the meantime enjoy terminal-notifier, it’s a great tool.