Send an Alert to Notification Center from the Command Line in OS X

Aug 3, 2012 - 17 Comments

Send Alert to Notification Center 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:

./terminal-notifier.app/Contents/MacOS/terminal-notifier

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"

Notification Center popup alert as posted from the command line

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 http://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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

17 Comments

» Comments RSS Feed

  1. Jason says:

    Does this send a desktop notification as well, or does it only post to the Notification Center panel? Looks great will check it out later.

  2. Napoleon Wils0n says:

    You shouldnt use special characters like ! inside double quotes because of shell expansion

    -bash: !: event not found

  3. Daniel says:

    The terminal command notification sounds like a great idea but can anyone explain how to make it work?

  4. William Scott says:

    Here is one example:

    Say I want to run some command that takes several minutes, so I want to do something else while it runs but receive notification when it is done. I also hate the terminal bell sound, so I turned it off. I can use this as follows:

    In bash or zsh, I can define a function like this:

    tnot () { “$@” && terminal-notifier -message “$( echo $@ ) returned $?” -title “Command $HISTCMD Completed” >/dev/null ; }

    Now, I can issue

    tnot sleep 12

    It runs the sleep command (waits 12 seconds), and when it has finished, it posts the notification.

    Now I need to figure out how to bind it to something like option-return, so I can invoke it without typing anything.

  5. terminal-notifier is only supported on Mac OS X 10.8, or higher.

    :(

  6. Tapeseries5 says:

    How to remove it ??

  7. alain says:

    Is there any way to get those notifications on all devices? Maybe sent the message to my icloud account?

  8. Aaron says:

    Doing “command && terminal-notify” will only display the notification if the command succeeds (i.e., returns 0). If you want it to show regardless, use ; instead of && (like “command; terminal-notify”). Alternately, if you want to notify only when the command fails, use || (like “command || terminal-notify”). If you want more advanced logic than that, I suggest you read up on how to do if-then clauses in bash scripting.

  9. Aaron says:

    Unless I missed it, this doesn’t seem to have a way to make the notification stay on the screen, instead of sliding into the notification center after a couple of seconds.

  10. James says:

    Change the behaviour in System Preferences. Find terminal-notifier listed in the Notifications pref pane, and set the alert style to Alerts, or Banners.

  11. amin ghadesi says:

    terminal-notifier -message “Go to OSXDaily.com, it’s the best website ever!” -title “osxdaily.com” -open http://osxdaily.com

    chang to:

    terminal-notifier -message “Go to OSXDaily.com, it’s the best website ever\!” -title “osxdaily.com” -open http://osxdaily.com

  12. ScoobyDoo says:

    Has anyone been able to make this work in 10.9? I installed it, have Notifications enabled in SysPref but I’m not getting any notifications showing from the terminal either with or without sudo.

  13. Samy says:

    @Scooby
    yes working in 10.9.2 here

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates