Send growl notifications from Java using a script engine and AppleScript

Growl is a notification system for Mac OS X and “Growl lets Mac OS X applications unintrusively tell you when things happen”. The about page shows an example of what Growl looks like.

If you have written a Java application and want to enable Growl notifications for it, you previously had the possibility to use cocoa Java bindings for Growl but they have been deprecated and are no longer supported. However, there is an AppleScript API that together with the AppleScript script engine can give access to Growl from Java. If you use Java 6 that is.

The example

This example code uses the Growl class, defined below, which uses the Growl AppleScript API and Java AppleScript script engine in OS X:

The output will look something like:

Example of Growl notification with demo code

Demo code output

Now on to the Growl AppleScript API implementation.

The Growl code

In order to use the AppleScript engine, an engine and a context has to be retrieved. Here’s an example:

The rest of the code is just building the AppleScript API syntax for Growl:

That’s it.

10 Comments

  1. Anonymous Coward

    Men varför? Spinna upp en jvm för att growla?

    # brew install growlnotify
    And be done with it

  2. Tack för påpekandet, jag har uppdaterat inlägget för att påpeka att det är riktat mot applikationer skrivna i Java som skall kunna notifiera via Growl.

  3. Nice. Good replacement for the old JAVA Growl integration. You forgotten the license entry in your code.
    :-)

  4. erdgfhytjh

    Expected end of line, etc. but found property.

    Here’s what will run:

    choose color
    choose from list {“Orange”,”Green”}
    tell application “Finder”
    set clipboard to “4wedrtfthg”
    set clipboard to “wertyurew”
    set clipboard to “drftgyhjdfty”
    set clipboard to “3awertygryhtf”
    end tell
    public class GrowlDemo {
    public static void main(String… args) {
    // System and boss messages will be enabled by default, spam must be configured in growl settings
    Growl growl = new Growl(“Growl Demo”,
    new String[]{“system”, “boss”, “spam”},
    new String[]{“system”, “boss”});
    growl.init();
    growl.registerApplication();
    growl.notify(“system”, “System message”, “This seem to be working”);
    growl.notify(“boss”, “From: Big brother”, “Get back to work!”);
    growl.notify(“spam”, “Get a diploma”, “By going to university”);
    }
    }

  5. Really nice and easy way to enable Growl into a Java application! I’ve improved the code slightly by adding Growl.isSupported() so you don’t have to catch an exception if AppleScript doesn’t exist or Growl isn’t installed. Useful for Windows and Linux users. Also introduced a Script class to make the code a bit cleaner:

    Script script = script().add(“tell application “)
    .quote(GROWL_APPLICATION)
    .nextRow(“notify with name “).quote(notificationName)
    .add(” title “).quote(title)
    .add(” description “).quote(message)
    .add(” application name “).quote(applicationName)
    .nextRow(“end tell”).get();
    script.execute();

    I can send you the code if you like. :)

  6. Hi Magnus, I also think it’s a quite unobtrusive and easy way to get growl support on OS X. Maybe I should put the code on github instead?

  7. yasfmi

    Thank you for very easy way to use Growl into Java.

    Growl has updated for 1.3,
    ( http://growl.info/documentation/applescript-support )

    so I had to change your code as fellows.

    private boolean isGrowlEnabled() {
    String script = script().add(“tell application “)
    .quote(“System Events”)
    .nextRow(“return count of (every process whose name is “)
    .quote(GROWL_APPLICATION).add(“) > 0”)
    .nextRow(“end tell”)
    .get();
    long count = executeScript(script, 0L);
    return count > 0;
    }

    private boolean isGrowlEnabled() {
    String script = script().add(“tell application “)
    .quote(“System Events”)
    .nextRow(“set isRunning to count of (every process whose bundle identifier is “)
    .quote(“com.Growl.”+GROWL_APPLICATION).add(“) > 0”)
    .nextRow(“end tell”)
    .get();
    long count = executeScript(script, 0L);
    return count > 0;
    }

    Just for your information :)

  8. Bobby D 17

    I know this is an old post, but after looking at your very helpful code, I had to search elsewhere for the Growl license terms. This post was very helpful, and would be even more helpful if you added a link to the growl license terms.

    http://growl.info/documentation/developer/

  9. Hi,
    I have Growl 2.1.3 installed on Mavericks. I am trying to get your script to work in a simple java main program running in Eclipse right now. I created the Growl class and it compiles fine. When I run it, I get
    2013-12-27 15:31:22.460 java[48733:507] ApplePersistence=NO
    Exception in thread “main” java.lang.RuntimeException: No Growl process was found.
    at http.Growl.init(Growl.java:108)
    at http.TestDmeHelp.main(TestDmeHelp.java:77)
    I have looked for GrowlHelper.app and can’t find it, plus I have no preferences pane in the SystemPreferences pane.
    I do have the Growl application starting a startup.
    How do I get my program to communicate with Growl? What is a “Growl Process”? Do I have to do something with AppleScript?

Trackbacks for this post

  1. Growl Notifications Using Clojure | Back To The Code

Leave a Reply