applescript

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

Table of Contents1 - A “Hello, world” MacOS AppleScript notification2 - AppleScript: Mac notification with a sound3 - How to add a title and subtitle to your notification4 - Running from Scala or Java5 - See also6 - Summary

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.

Simple iTunes AppleScript script examples

If you’ve never used AppleScript, here are two iTunes AppleScript examples to get you going. First, This one tells iTunes to play the playlist named “My Favorites”:

tell application "iTunes"
    play playlist "My Favorites"
end tell

That script starts playing a random song from that playlist. If you want to start by playing the first song of that playlist, this script will do the trick:

How to size (or resize) application windows in AppleScript

AppleScript FAQ: How can I size or resize an application window using AppleScript?

To resize an application window with AppleScript, use a command like this:

tell application "Safari"
    set bounds of front window to {300, 30, 1200, 900}
end tell

That command tells the Safari browser to have the following size and location properties:

A simple way to show many stock quotes and charts

Many moons ago I thought I wrote an AppleScript script named GetStockUrls, whose sole purpose was to open many webpages from finance.yahoo.com at one time. I could run that script, then easily look at the stocks I owned.

Today I found that script on an old Mac computer, and when I did that I saw that I didn’t write it with AppleScript, but instead created it with the Mac Automator. This image shows all you have to do in the Mac Automator to achieve this result. On my current Mac this script opens the Safari browser and opens each URL shown in a new tab. I can then move between the tabs to see what I want to see, quickly and easily.

This image shows what the result looks like in the Safari browser:

How to execute AppleScript from a Java or Scala application

If you ever need to execute AppleScript from a Java or Scala application, this code shows how to solve the programming part of this problem.

Given an AppleScript command as a Scala multiline string, like this:

// press the `fn` key twice
val asCommand = """
tell application "System Events"
    key code 63
    key code 63
end tell
"""

you can execute that command by calling this executeAppleScriptCommand method:

How do I find out what application elements can be scripted with AppleScript?

AppleScript FAQ: How do I find out what application elements can be scripted with AppleScript?

Open up the AppleScript Editor (Applications > Utilities > AppleScript Editor), then go to File > Open Dictionary, and select the application you want to learn more about. For instance, I just wanted to learn more about scripting iTunes, so I chose iTunes from the dialog that was display.

Here’s what that AppleScript Editor “Dictionary’ dialog looks like:

Java Mac AppleScript ScriptEngine null (ScriptException)

A funny thing about using AppleScript with Java -- and three years later with Scala and Akka -- is that you tend to run into the same errors. So glad this URL kept a transcript of my earlier problem ("Problems running AppleScript with Java headless argument"), the solution of which is shown in this image. The conversation was also logged in a less convenient format here.