Skip to content, Skip to search

Changes

Jython Scripting

1,990 bytes added, 16:23, 25 September 2009
Tips and Tricks
[[Category:Scripting]]
[[Category:Jython]]
 
 
== Jython for plugins ==
 
=== Using a jython script as a plugin ===
 
The simplest way is to place the jython script file into fiji/plugins/ folder or a subfolder, and it will appear in the menus on restarting fiji.
 
 
=== Distributing jython scripts in a .jar file ===
 
PLEASE NOTE: there is no need to do the following. See entry above.
 
We create two jython scripts that we want to distribute in a .jar file as plugins:
 
The printer.py script:
 
<source lang="python">
IJ.log("Print this to the log window")
</source>
 
The create_new_image.py script:
 
<source lang="python">
ip = ByteProcessor(400, 400)
imp = ImagePlus("New", ip)
ip.setRoi(OvalRoi(100, 100, 200, 200))
ip.setValue(255)
ip.fill(ip.getMask())
imp.show()
</source>
 
Place both scripts under a folder named <i>scripts</i>.
 
You will need a tiny .java file specifying a launcher PlugIn, such as:
 
<source lang="java">
package my;
import ij.plugin.PlugIn;
import Jython.Refresh_Jython_Scripts;
 
public class Jython_Launcher implements PlugIn {
public void run(String arg) {
new Refresh_Jython_Scripts.runScript(getClass().getResourceAsStream(arg);
}
}
</source>
 
Notice we place the above file under directory <i>my/</i>, packaged.
 
To compile it:
 
$ java -cp .:ij.jar my/Jython_Launcher.java
 
 
Then we define the plugins.config file:
 
Plugins>My Scripts, "Print to log window", my.Jython_Launcher("/scripts/printer.py")
Plugins>My Scripts, "Create image with a white circle", my.Jython_Launcher("/scripts/create_new_image.py")
 
Finally, we put all files in a .jar file:
 
$ jar my_jython_scripts.jar plugins.config my/Jython_Launcher.class scripts/*py
 
Then, drop the jar file into fiji/plugins/ folder and run "Help - Update Menus", or restart fiji. Your scripts will appear under Plugins - My Scripts.
 
Notice, though, that <b>you don't need to do the .jar packaging at all</b>. Just place the python scripts directly under fiji/plugins/My Scripts/ and they will appear in the menus as regular plugins.
Emailconfirmed, uploaders
653
edits