package imagej.ext.script;

import imagej.ext.plugin.ImageJPlugin;
import imagej.ext.plugin.Parameter;
import imagej.ext.plugin.Plugin;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Executes a script.
 * Using the file extension to choose the appropriate engine.
 * 
 * @author Johannes Schindelin
 * @author Grant Harris
 */
@Plugin(menuPath = "Plugins>Script>Run Script")
public class ScriptPlugin implements ImageJPlugin {

	@Parameter
	protected File path;

	@Override
	public void run() {
		// TODO make a nice SezPoz-discoverable interface for scripting
		// languages
		final ScriptEngineManager scriptManager = new ScriptEngineManager();
		// Could use a FileChooser to select script, then
		String fileExtension = getFileExtension(path.getPath());
		final ScriptEngine engine = scriptManager.getEngineByExtension(fileExtension);
		try {
			engine.put(ScriptEngine.FILENAME, path.getPath());
			// TODO
			// Bind java objects to script engine and for script access
			// e.g. get current Display
			// scriptEngine.put("currentDisplay", currentDisplay) ;
			// same effect as: getBindings(ScriptContext.ENGINE_SCOPE).put. 
			final Object result = engine.eval(new FileReader(path));
			if (result != null) {
				System.out.println(result.toString());
			}
		} catch (final ScriptException e) {
			e.printStackTrace(System.err);
		} catch (final IOException e) {
			e.printStackTrace(System.err);
		}
	}

	String getFileExtension(String filePath) {
		File f = new File(filePath);
		String name = f.getName();
		int k = name.lastIndexOf(".");
		String ext = null;
		if (k != -1) {
			ext = name.substring(k + 1, name.length());
		}
		return ext;
	}

}

@@ -128,16 +128,46 @@
 .Plugin;
+%0Aimport imagej.util.FileUtils;
 %0A%0Aimport
@@ -898,16 +898,26 @@
 nsion = 
+FileUtils.
 getFileE
@@ -925,34 +925,24 @@
 tension(path
-.getPath()
 );%0A%09%09final S
@@ -1584,16 +1584,16 @@
 r);%0A
+
 %09%09%7D%0A%09%7D%0A%0A
 %09Str
@@ -1592,250 +1592,6 @@
 %09%7D%0A%0A
-%09String getFileExtension(String filePath) %7B%0A%09%09File f = new File(filePath);%0A%09%09String name = f.getName();%0A%09%09int k = name.lastIndexOf(%22.%22);%0A%09%09String ext = null;%0A%09%09if (k != -1) %7B%0A%09%09%09ext = name.substring(k + 1, name.length());%0A%09%09%7D%0A%09%09return ext;%0A%09%7D%0A%0A
 %7D%0A

Use the FileUtils class in ScriptPlugin