Java AppleScript: How to run a multi-line Applescript command from a Java program

Java AppleScript FAQ: How can I run a multiline MacOS AppleScript script from a Java application?

If you ever need to run a multi-line Applescript command from a Java program, fear not, it can be done.

A Java AppleScript example

Here’s an example of some Java code that runs multiple lines of Applescript, six to be precise:

Runtime runtime = Runtime.getRuntime();
String applescriptCommand =  "tell app \"iTunes\"\n" + 
                             "activate\n" +
                             "set sound volume to 40\n" + 
                             "set EQ enabled to true\n" +
                             "play\n" +
                             "end tell";

String[] args = { "osascript", "-e", applescriptCommand };
Process process = runtime.exec(args);

As you can see, that snippet of Java and Applescript code tells iTunes to activate itself, then sets the volume, and then starts playing.

Java AppleScript newline tip

One thing to note here is that it's very important to include those pesky \n characters at the end of each line in your Applescript code (except the last one). I can tell you from recent experience that your script won't run unless you include those lines.

Alternatively, I just learned that I can run a multiline AppleScript command from Java like this:

String[] args = { "osascript", "-e", 
    "tell application \"System Events\"",
    "-e",
    "set frontApp to name of first application process whose frontmost is true", 
    "-e",
    "end tell" };
Process p = runtime.exec(args);

This works just like the earlier command, and may be easier to construct. (That being said, I'm not going to take the time to rewrite the remainder of this tutorial, so my Java program below will use the previous Java/AppleScript syntax.)

A complete Java and Applescript sample program

Okay, while I'm here, I'll just share all the source code for this Java + Applescript program:

package com.devdaily.mac.applescript;

import java.io.IOException;

public class MultilineAppleScriptTest
{
  public static void main(String[] args)
  {
    new MultilineAppleScriptTest();
  }
  
  public MultilineAppleScriptTest()
  {
    Runtime runtime = Runtime.getRuntime();
    
    // an applescript command that is multiple lines long.
    // just create a java string, and remember the newline characters.
    String applescriptCommand =  "tell app \"iTunes\"\n" + 
                                   "activate\n" +
                                   "set sound volume to 40\n" + 
                                   "set EQ enabled to true\n" +
                                   "play\n" +
                                 "end tell";

    String[] args = { "osascript", "-e", applescriptCommand };

    try
    {
      Process process = runtime.exec(args);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

There's not too much to say about this Java/Applescript program, other than a few quick bullet points:

  • You create your multiline Applescript as a Java string.
  • Again, remember to include those newline characters at the end of each Applescript line.
  • The newline character isn't needed after the last Applescript line.

I hope this source code example helps in your own Java/Applescript efforts.

Applescript with Java SE 6

FYI: I just read that you can code this differently with Java SE 6 on the Mac OS X platform. This URL from Apple shows the new syntax you can use in Java SE 6, specifically this sample code:

public static void main(String[] args) throws Throwable {
    String script = "say \"Hello from Java\"";
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");
    engine.eval(script);
}

I haven't tested that, as I don't have Java SE 6, but hey, it comes direct from Apple.