Apple - How can I trigger a Notification Center notification from an AppleScript or shell script?

With Mavericks and later, you can do this using AppleScript's 'display notification':

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

                          

That's it—literally that simple! No 3rd-party libraries or apps required and is completely portable for use on other systems. 10.9 notification on the top, 10.10 DP in the middle, 10.10 on the bottom.

AppleScript can be run from the shell using /usr/bin/osascript:

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

You can also customise the alert further by adding…

  • a subtitle

    Append 'subtitle' followed by the string or variable containing the subtitle.

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

    The above example produces the following notification:

  • sound

    Append 'sound name' followed by the name of a sound that will be played along with the notification.

    display notification "message" sound name "Sound Name"
    

    Valid sound names are the names of sounds located in…

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

Posting notifications can be wrapped as a command-line script. The following code can be run in Terminal and will add a script to /usr/local/bin (must exist, add to $PATH) called notify.

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

This is the script that the above will add to notify.

#!/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

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.


I just want to add a note to @grgarside's answer, because I know many people want to have a notification popup that can only be dismissed by button click.

Comparison

I have found the solution for you:

It was actually controlled by a setting in System Preference, rather than some parameter in the script written. These two images should explain it all

Thanks @grgarside for the great answer.

System Preference