Part 1 - Introduction and example

Nov 11, 2004, VR. Embedding a scripting language such as Javascript in certain types of computer applications makes it much easier for the user to repeat complex tasks that may be customized.

Imaging applications, in particular, fall into this category. Processing images frequently require repeating nearly identical operations on a large number of images.

Image of the sun in white lightFor example, a solar photographer (such as myself) may take many images of the Sun daily. Processing these images is complicated, extremely repetitve, and quite time consuming. Each image must be cropped, aligned, stacked, enhanced, colorized, oriented, resized, and labeled.

 

 

 

 

An application could be developed to provide the needed operations and reduce the drudgery of performing them manually. Due to differing weather conditions, cameras, telescopes, lenses, or filters it may be necessary to adjust several parameters for each operation.  Adding scripting capability lets the user customize parameters for a sequence of image operations.There are a number of scripting languages and ways to embed them in a Java™ application. Javascript was chosen because the language is widely used and readily available from the Mozilla Rhino project (See http://www.mozilla.org/download.html). Apache BSF (See http://jakarta.apache.org/bsf/index.html ) provides a way to embed not only Javascript, but several other scripting language as well. Unfortunately, as of this writing there are incompatibilites between the current versions of BSF and the Mozilla Javascript library. To avoid conflicts, the approach used here was to access the Javascript library directly.

The following simple example illustrates how to execute Javascript from an application. Note that only three methods are called to execute the script. Method calls in Lines 32 and 33 initialize the JavaScript environment. Line 37 actually executes the script.

In the next article, we'll develop a basic image library and show how to access it with embedded JavaScript.

Go to article index


1   /* 
2    * Copyright (c) 2004, Raben Systems, Inc.
3    * SimpleScript.java
4    *
5    * Created on November 4, 2004, 3:23 PM
6    */
7
8   package com.raben.example.imagescript;
9   import org.mozilla.javascript.Context;
10  import org.mozilla.javascript.Scriptable;
11  import org.mozilla.javascript.JavaScriptException;
12
13  /***
14   * Simple example of how to call Mozilla JavaScript
15   * @author  Vern Raben
16   */
17  public class SimpleScript {
18
19      /***
20       * @param args Not used
21       */
22      public static void main(final String[] args) {
23
24          // Create a simple JavaScript program
25          StringBuffer script = new StringBuffer();
26          script.append("var x = 1;\n");
27          script.append("var y = 2;\n");
28          script.append("var z = x + y;\n");
29          script.append("java.lang.System.out.println(\"z=\" + z);\n");
30
31          // Initialize the execution environment
32          Context cx = Context.enter();
33          Scriptable scope = cx.initStandardObjects(null, true);
34
35          try {
36              // Execute the JavaScript program
37              cx.evaluateString(scope, script.toString(), "<cmd>", 1, null);
38          } catch (JavaScriptException e) {
39              System.err.println(e);
40          }
41      }
42
43  }