Summary: This tutorial demonstrates how to fire macOS system notifications with AppleScript and Scala (or Java).
In this article it helps if you already know a little bit about AppleScript, though that’s not completely necessary. Near the end of the tutorial I show how to invoke the AppleScript code using Scala, so feel free to skip down to there if you just want to see that — you can always read the stuff at the top for reference later.
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.
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
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.
Running from Scala (or Java)
A great thing is that 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" with title "Scala" sound name "Purr" """ // run the command val runtime = Runtime.getRuntime val code = Array("osascript", "-e", command) val process = runtime.exec(code)
With MacOS 10.14.5 and Scala 2.12.8 the notification looks like this:
Those lines of code are simple, so you can easily test them from the Scala REPL:
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.
I originally tested this technique on Mac OS X 10.10 using Scala 2.11, and it still works in 2019 with MacOS 10.14.5 and Scala 2.12.8. (No import statements are required.)
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.
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |
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:
- Use AppleScript to display a dialog from the Mac OS X Unix shell
- How to run an AppleScript from the Mac Unix shell
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.