Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'd love to be able to take advantage of 10.8's Notification Center features in AppleScripts and shell scripts I write.

Is there a built-in command or a third-party library I can use from either an AppleScript or shell script?

Ideally the type and icon of the notification could be controlled, but even just the ability to trigger a basic banner with a stock icon (but custom text) would be appreciated.

share|improve this question

4 Answers 4

up vote 45 down vote accepted

With Mavericks, you can do this using AppleScript:

display notification "Lorem ipsum dolor sit amet" with title "Title"

That's it — literally that simple! No 3rd-party libraries or apps required.
10.9 notification on the left, 10.10 on the right.

From Terminal:

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

You can also add…

  • a subtitle

    display notification "message" with title "title" subtitle "subtitle"
    

  • sound

    display notification "message" sound name "Sound Name"
    

    Valid sound names are the names of sounds located in…

    • ~/Library/Sounds
    • /System/Library/Sounds

Wrap as a command-line script:

cd /usr/local/bin && echo -e "#!/bin/bash\n/usr/bin/osascript -e \"display notification \\\"\$*\\\"\"" > notify && chmod +x notify;cd -

Will output the following code to /usr/local/bin (must exist) and add executable flag.
Make sure /usr/local/bin is in your $PATH.

#!/bin/bash
/usr/bin/osascript -e "display notification \"$*\""

Now to display a notification:

notify Lorem ipsum dolor sit amet
sleep 5 ; notify Slow command finished
share|improve this answer
    
What is the path of display? I have Mavericks but I can't see to find it. Thanks! –  ceiling cat May 2 at 18:09
    
@ceilingcat Could you clarify what you mean? What doesn't work? –  George Garside May 2 at 18:11
    
I got -bash: display: command not found My path is kind of screwy though, so it would be nice to know exactly where display is located. –  ceiling cat May 2 at 18:31
    
@ceilingcat This is AppleScript. To run it in Terminal, use osascript (see answer). –  George Garside May 2 at 18:32
    
oh... I missed that part. Thanks! –  ceiling cat May 2 at 18:34

terminal-notifier

From the README:

terminal-notifier is a command-line tool to send Mac OS X User Notifications, which are available in Mac OS X 10.8.

It is currently packaged as an application bundle, because NSUserNotification does not work from a ‘Foundation tool’. radar://11956694

This tool will be used by Kicker to show the status of commands which are executed due to filesystem changes. (v3.0.0)

Download

Prebuilt binaries, which are code-signed and ready to use, are available from the downloads section.

Usage

$ ./terminal-notifier.app/Contents/MacOS/terminal-notifier group-ID sender-name message [bundle-ID]

In order to use terminal-notifier, you have to call the binary inside the app bundle.

The first argument specifies the ‘group’ a notification belongs to. For any ‘group’ only one notification will ever be shown, replacing previously posted notifications. Examples are: the sender’s process ID to scope the notifications by a unique process, or the current working directory to scope notifications by a project.

The second and third arguments describe the notification itself and are its ‘title’ and ‘message’ respectively. For example, to communicate the sender of a notification to the user, you could specify the sender’s name as the title.

The fourth and last argument is an optional one. It specifies which application should be activated when the user clicks the notification. By default this will activate Terminal.app, to launch another application instead specify the application’s bundle identifier. For example, to launch Safari.app use: com.apple.Safari.

share|improve this answer
    
Sweet, a great library! Would love to see it updated with control over the icon and alert vs banner, but definitely useful. Thanks for the link! i.imgur.com/tsCeK.png –  Nathan Greenstein Jul 25 '12 at 20:27
2  
Great little app, I created a launcher and put in /usr/local/bin so that I can use it anywhere from a terminal etc. Launcher script: #!/bin/bash cd /Applications/terminal-notifier.app/Contents/MacOS ./terminal-notifier $* –  johanandren Jul 26 '12 at 7:13
4  
Launcher is helpful, if you change it a bit it works with quoted text with spaces: #!/bin/bash cd /Applications/terminal-notifier.app/Contents/MacOS ./terminal-notifier "$@" –  Redbeard Jul 31 '12 at 1:34

You can now do this with Growl2 (available from the App Store). Install Growl and enable "OS X Notifications" (screenshot)

enter image description here

Additionally, you'll need to install GrowlNotify for a command-line tool to send Growl notifications. You can download this tool for free on the Download page.

You can now generate Growl notifications from the command line, which will be forwarded to the Notification Center. For example:

growlnotify -n "My App" -m "Hello world"

enter image description here

Note that it currently doesn't seem possible to change the application icon.

share|improve this answer
    
Nice approach especially if growl is installed already. –  Besi Oct 25 '12 at 12:34
    
I think you might be able to use the option --icon path/to/icon.png for icons. –  Will Feb 12 at 17:37

Since Growl is not free, and terminal-notifier is not available on pre-10.8 systems, I tend to use cocoaDialog. It is free and open source, so you can probably also distribute along with your scripts.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.