How to fire Mac OS X notifications with AppleScript (and Scala)

Summary: This tutorial demonstrates how to fire MacOS system notifications with AppleScript (and Scala or Java).

In this article I assume that you already know at least a little bit about how to use AppleScript, and just want to know how to trigger a MacOS notification. At the end of the tutorial I show how to invoke the AppleScript code using Scala and Java.

Back to top

A “Hello, world” MacOS AppleScript notification

To get started, here’s the code for a basic “Hello, world” AppleScript notification:

display notification "Hello, world"

The code looks like this in the AppleScript Editor:

Running this code creates a notification in the MacOS Notification Center that looks like this:

If you’re comfortable writing AppleScript, this part is straightforward.

Back to top

AppleScript: Mac notification with a sound

You can add sounds to your notifications by adding the sound name command, like this:

display notification "Hello, world" sound name "default"

With this code, the notification window looks the same as before, and you’ll also hear a sound when it’s displayed. I don’t like the default sound, so I dug around and found that you can use the names of sounds in the /System/Library/Sounds folder. I have these sounds on my current OS X 10.9 system:

Basso.aiff     -- good, but error-like (low keys on keyboard)
Blow.aiff      -- good
Bottle.aiff    -- too short
Frog.aiff      -- chirp
Funk.aiff      -- thud
Glass.aiff     -- good (like the end of a timer)
Hero.aiff      -- good
Morse.aiff     -- 'pop'
Ping.aiff      -- good
Pop.aiff       -- shorter 'pop'
Purr.aiff      -- good
Sosumi.aiff    -- good
Submarine.aiff -- good
Tink.aiff      -- too quiet

Just use the first part of each file name to trigger the desired sound. For instance, the “Purr” sound is good, so my notification code looks like this:

display notification "Hello, world" sound name "Purr"

You can see the sounds available on your system with this ls command (using the Terminal):

ls /System/Library/Sounds
Back to top

How to add a title and subtitle to your notification

You can also add a title and subtitle to your notifications. For example, this AppleScript code:

display notification "Hello, world" with title "Hello" subtitle "world"

yields this notification:

You can add sound name to that command as well.

Back to top

Running from Scala or Java

You can run AppleScript scripts from Scala and Java as well. The process looks like this:

// define your applescript command
val command = """display notification "Hello, world" sound name "Purr" """

// run the command
val runtime = Runtime.getRuntime
val code = Array("osascript", "-e", command)
val process = runtime.exec(code)

There are other ways to do this, but I’ve had problems getting those approaches to run without errors on different MacOS versions, so at the moment, I use this approach in my AppleScriptUtils project.

Note: I just tested this on Mac OS X 10.10 using Scala 2.11, and it still works as shown. (No import statements are required.)

Back to top

See also

This link on Apple’s website is a good place to start looking for more information. Just search for “notification” on that page, and follow the links.

Back to top

Summary

If you’re comfortable with Mac OS X programming, you may also know that you can run commands like this from the Unix shell. I show how to run AppleScript from the Unix command line and Unix scripts in these two articles:

In summary, if you were looking for an example of how to trigger a notification in the Mac OS X notification center using AppleScript, I hope this has been helpful.

Back to top

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.