Calling an AppleScript application from a Java Program

Code Example

I am trying to call an AppleScript application from a Java program. I found this code example:

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();
    }
  }
}

Problem

When searching for the package "com.devdaily.mac.applescript" it references in Eclipse for installing new software, it does not find the package or even just searching for "applescript" in general. So, this must be a one off custom package the author created. Any ideas on locating this package OR do you know of any other examples I can try that performs a similar purpose? Thank you!

Replies

The code shown uses the osascript command-line tool to run the AppleScript. I don’t think that needs any fancy packages; you should be able to run it like any other command-line tool.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you, it worked with using the ProcessBuilder Class.

Code

...
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("osascript", "<path>/<applescript name>.app");
try {
    Process process = processBuilder.start();
    ...