AppleScript from Java on Mac OS X 10.6?

Deadly 提交于 2019-12-01 11:12:37

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

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!