3

To call AppleScript from Python, I use the "appscript" bridge:

http://appscript.sourceforge.net/

What can I use to call AppleScript from Java on Mac OS X 10.6+?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Maroloccio
  • 1,143
  • 2
  • 12
  • 22

2 Answers2

3

Here's an approach that works for me for Java on Mac OS X 10.6+. This example script fetches the creation date of the current folder:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.util.Date;
import java.util.GregorianCalendar;

public class ScratchSpace {

    public static void main(String[] args) throws ScriptException {
        System.out.println("creationDate = " + getFileCreationDate(new File(".")));
    }

    private static Date getFileCreationDate(File file) throws ScriptException {
        final String script = "set myfile to \"" + file.getAbsolutePath() + "\"\n" +
                "set myinfo to info for myfile\n" +
                "creation date of myinfo";
        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("AppleScript");
        final GregorianCalendar result = (GregorianCalendar) scriptEngine.eval(script);
        return result.getTime();
    }

}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
0

I don't know Java, but anything that can execute a command line tool can execute an AppleScript using osascript. I use it to execute AppleScripts from PHP and vim scripts.

Chuck
  • 4,662
  • 2
  • 33
  • 55
  • I am looking for an Apple event bridge like Appscript, not a "Runtime.exec()" -like workaround. Appscript in Python allows things like: app('TextEdit').documents['ReadMe'].paragraphs[1].get(). – Maroloccio May 29 '11 at 03:46