Toying with Java and AppleScript

I am playing with writing a JavaFX UI on a little utility application that I want to write. The utility app is a simplification of the interface to OmniFocus. I use OmniFocus for a super todo list and occasionally need a simple view of that list. This is a bit of an odd technology choice. Mostly, I am playing with JavaFX and want to make it do something useful.

So, in the process of exploring how I am going to make this work I was playing with using applescript from java. The older mechanisms that I found on most web sites uses the formerly deprecated, now deleted, Cocoa-Java Bridge. So, I started down the path of using Rococoa. However, I ran across something much simpler. Java 6, on the mac, supports a ScriptEngine plugin for apple script. Here is an example:

import javax.script.*;

public class AppleScriptTest{
	public static void main(String[] args){
		String script = "tell application \"Finder\" \n"
		     + " get the name of every item "
		     + " in the desktop \n" + "end tell";

		ScriptEngineManager mgr = new ScriptEngineManager();
		ScriptEngine engine = mgr.getEngineByName("AppleScript");
		try{
			Object obj = engine.eval(script);
			for(Object o : (java.util.ArrayList)obj){
				System.out.println(o);
			}
		}catch(ScriptException se){
			se.printStackTrace();
		}
	}

}

Here is another example, this time talking to OmniFocus

import javax.script.*;

public class AppleScriptTest{
	public static void main(String[] args){
		String script = "tell application \"OmniFocus\"\n"
				+ "tell default document\n"
					+ "set theContext to the context \"Office\"\n"
					+ "tell quick entry\n"
						+ "set theTask to make new inbox task with properties {name:\"A Task\", note:\"The Body\", context:theContext}\n"
						+ "select {theTask}\n"
						+ "open\n"
					+ "end tell\n"
				+ "end tell\n"
			+ "end tell";

		ScriptEngineManager mgr = new ScriptEngineManager();
		ScriptEngine engine = mgr.getEngineByName("AppleScript");
		try{
			engine.eval(script);
		}catch(ScriptException se){
			se.printStackTrace();
		}
	}

}

I’ll write more as I get further into this. However, I think that this will make for a very promising bridge to the application.


Related Posts with Thumbnails
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • DZone
  • Facebook
  • Furl
  • Google Bookmarks
  • YahooBuzz
  • YahooMyWeb

About this entry