JavaScript Runner


Your browser is ignoring the <APPLET> tag!

Your browser is ignoring the <APPLET> tag!

H-P Ulven 12.03.08, created with Geogebra (www.geogebra.org) and Rhino (www.mozilla.org/rhino/) and BlueJ (www.bluej.org)


Documentation:

Rhino is an implementation of JavaScript (ECMAScript) in Java. I have tried to embed Rhino in an applet (JS Runner) which communicates with GeoGebra, which makes JavaScript a kind of macro language for GeoGebra. This might have some use for GeoGebra users...?

Important:
The JavaScript in JS Runner does not communicate with the browser, only with GeoGebra. So, if you want to do something with the html-page, use ordinary JavaScript embedded in the HTML page.

This also implies that the ordinary connection to GeoGebra via the document object is not possible (document.ggbApplet.evalCommand(...) or document.applets[0].evalCommand(...)).

Use: ggb.evalCommand(...), ggb.getValue(...), ..., as I have made GeoGebra available via the global variable ggb.

And, for the same reason, writing to the console (Java Console) has to be done via Java, like this:

out=java.lang.System.out;
out.println(...)

Possible use:

How to use:

You can choose between sending JavaScript (radiobutton "JS") or GeoGebra commands (radiobutton "Ggb cmds") to GeoGebra.

You can also choose between sending from the editor window (radiobutton "From window") or from a file (radiobutton "From file").

All execution with the "Run"-button.

Clear editor window with "Clear"-button

Load and save editor window from/to file with the buttons "Load" and "Save"

Examples:

JavaScript:
// JavaScript:
// Make som objects, and list their names
out=java.lang.System.out;	//Use java Console for output
ggb.evalCommand("a=33");		//Make objects...
ggb.evalCommand("P=(2,3)");
ggb.evalCommand("Circle[P,3]");
n=ggb.getObjectNumber();		//List all objects names
for(i=0;i<n;i++){
out.println(ggb.getObjectName(i));
}

Copy (Ctrl-C) and paste (Ctrl-V) this into the JS Runner window and click "Run"!

The Java Console (turn on in browser) should show the list of names; a, P and c.

(The "Result undefined" in the end means that the last for-statement does not return any value.)

Ggb commands:
a=33
P=(2,3)
c=Circle[P,3]

Copy and paste these ordinary Ggb commands into the JS Runner window, change "JS" to "Ggb cmds" and click "Run".

This is only execution of a list of commands, no comments or programming statements in this mode, but this mode might still be usefull to quickly put up some objects or quickly change the position of several objects during a demonstration.

How to install and use on your own computer and your own server:

It is difficult to make this work in the geogebra upload areas, as this kind of tools is a security risk the geogebra site does not allow.

Here are the files: jsrunner.zip .

Pack out in a directory.

To run as applets in a browser: Study the source in theJs_Runner.html file!

To run as local application, use the file jsrunner.bat.
(This starts JS Runner and GeoGebra in a fake browser. The problem with this, is that this fake browser is not able to do everything a normal browser does.
There is a peculiar bug with the toolbar, so they don't work very well in this mode...to put it mildly...
This is the reason I hope GeoGebra in time will get a normal application API and a plugin system, which would solve this problem.)

The good news:

The whole Java Framework (all the Java classes) are available for the scripts, try this:

/*
* Swing demo
*/
f=new Packages.javax.swing.JFrame("Title");
btn=new Packages.javax.swing.JButton("Click");
f.add(btn);
f.setSize(100,100);
f.setVisible(true);

(Remember to change to "JS" again :-) )

The certificate system in Java also allows scripts to access files, something which is difficult to do with ordinary JavaScript.

The bad news:

There are some bugs I have not been able to solve yet...sorry...

If you have the patience to try this, I welcome comments in the GeoGebra forums.