Display Notification Center Alerts from command line and AppleScripts

Sep 04, '12 07:30:00AM

Contributed by: Sesquipedalian

Using the Automator action from this recent hint, it is easy to post notifications from AppleScripts or the command line.

To set this up, simply do the following:

  1. Download and install the action.
  2. Create a new workflow in Automator containing only the Display Notification Center Alert action.
  3. In the variables section at the bottom of the workflow construction area in Automator's window, create three variables named title, subtitle, and message.
  4. Give message a default value like "Notification sent." You can leave the default values other two blank.
  5. Drag each variable to its corresponding field in the Display Notification Center Alert action.
  6. Save the workflow as Display Notification.wflow. A good place to save it is in ~/Library/Workflows (create the folder if necessary).

Now you can display a notification from the command line using the following command:
automator -D title='Title text' -D subtitle='Subtitle text' -D message='Message text' ~/Library/Workflows/'Display Notification.wflow'

To display notifications from AppleScripts, paste the following handler into your script, and call it using the form notify("Notification title", "Notification subtitle", "Notification message")

on notify(title, subtitle, message)
	if title as text is not "" then set title to " -D title=" & quoted form of (title as text)
	if subtitle as text is not "" then set subtitle to " -D subtitle=" & quoted form of (subtitle as text)
	if message as text is not "" then set message to " -D message=" & quoted form of (message as text)
	
	do shell script "automator" & title & subtitle & message & " ~/Library/Workflows/Display Notification.wflow"
end notify

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120831112030251