How can I trigger a Notification Center notification from an AppleScript or shell script?
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.
Solution 1:
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
Solution 2:
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://11956694This 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
.
Solution 3:
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.
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.
Solution 4:
You can now do this with Growl2 (available from the App Store). Install Growl and enable "OS X Notifications" (screenshot)
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"
Note that it currently doesn't seem possible to change the application icon.