Difference between revisions of "Scripting"

(Running Scripts in Headless Mode: May '09 is no longer "quite recently"...)
(Adding JAR-packaged scripts to the menu: Small fixes)
(Tags: Mobile edit, Mobile web edit)
 
(45 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{Learn|scripting}}ImageJ allows you to write scripts in several different languages.
 +
 +
= Getting started =
 +
 +
* Press the {{key|[}} key to open the [[Script Editor]] (or {{key|Shift}}-{{key|[}} to open the [[Script Interpreter]]).
 +
* Optionally, choose a template from the ''Templates'' menu to get you started.
 +
* Otherwise, choose your language from the ''Language'' menu.
 +
* Grab code snippets for common tasks from the [[Scripting toolbox]].
 +
* See [[Scripting comparisons]] for a side-by-side comparison of scripting languages.
 +
* See [[:Category:Scripting]] for a list of all scripting-related pages on this wiki.
 +
 +
= Supported languages =
 +
 +
ImageJ's [[Script Editor]] supports many different languages. The following table summarizes the possibilities.
 +
 +
{| class="wikitable"
 +
| colspan=2 style="background: #ddd; text-align: center; font-weight: bold" | Recommended options
 +
|-
 +
! [[Groovy Scripting|Groovy]]
 +
| [[wikipedia:Groovy (programming language)|Groovy]] is a flexible and powerful scripting language, Java-like but less verbose and dynamically typed. Learn this, and using Java later (if needed) will become easier.
 +
|-
 +
! [[Introduction into Macro Programming|ImageJ Macro]]
 +
| style="white-space: normal !important" | The [[ImageJ 1.x]] macro language is less powerful than the other scripting languages, but is designed to be easy to learn and use.
 +
|-
 +
! style="white-space: nowrap" | [[Jython Scripting|Python (Jython)]]
 +
| [[wikipedia:Python (programming language)|Python]] is a popular choice among scientists.
 +
|-
 +
! [[Javascript Scripting|JavaScript]]
 +
| [[wikipedia:JavaScript|JavaScript]] is a popular choice among web developers.
 +
|-
 +
! [[JRuby Scripting|Ruby (JRuby)]]
 +
| [[wikipedia:Ruby (programming language)|Ruby]] is another popular choice among web developers.
 +
|-
 +
! [[Clojure Scripting|Lisp (Clojure)]]
 +
| [[wikipedia:Lisp (programming language)|Lisp]] is a popular choice among computer scientists.
 +
|-
 +
! [[Renjin Scripting|R (Renjin)]]
 +
| [[wikipedia:R (programming language)|R]] is a popular choice among scientists and statisticians.
 +
|-
 +
| colspan=2 style="background: #ddd; text-align: center; font-weight: bold" | Other options
 +
|-
 +
! [[Introduction into Developing Plugins|Java]]
 +
| You can code Java plugins in the Script Editor. This is the most difficult path, but also the most powerful.
 +
|-
 +
! [[MATLAB Scripting|MATLAB]]
 +
| ImageJ can interface bidirectionally with MATLAB. See the [[MATLAB Scripting]] page for details.
 +
|-
 +
! [[Beanshell Scripting|BeanShell]]
 +
| [[wikipedia:BeanShell|BeanShell]] is an old script language, maintained mostly for backwards compatibility. It is nearly 100% compatible with Java syntax, but so is [[Groovy]].
 +
|-
 +
! [[Scala Scripting|Scala]]
 +
| [[wikipedia:Scala (programming language)|Scala]] support is currently experimental, and has bugs.
 +
|}
 +
 +
= Script parameters =
 +
 +
There is a universal <code>@parameter</code> notation available across all scripts for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x <code>GenericDialog</code> because it is totally agnostic to the user interface, allowing such scripts to run in a variety of contexts.
 +
 +
See the [[script parameters]] page for details.
 +
 
= Using an interpreter =
 
= Using an interpreter =
  
Line 5: Line 65:
 
== General key bindings ==
 
== General key bindings ==
  
* up arrow: bring the previously typed command.
+
* {{Key|up}}: bring the previously typed command.
* down arrow: bring the next typed command.
+
* {{Key|down}}: bring the next typed command.
* enter or return: execute the contents of the prompt.
+
* {{Key|enter}} or {{Key|return}}: execute the contents of the prompt.
  
== Multiline editing and keybidings==
+
== Multiline editing and keybindings==
  
 
You can enlarge the prompt by dragging the middle bar.
 
You can enlarge the prompt by dragging the middle bar.
  
* shift + enter: create a new line within the prompt.
+
* {{key|Shift||Enter}}: create a new line within the prompt.
* shift + up arrow: move to the line above within the prompt.
+
* {{key|Shift||Up}}: move to the line above within the prompt.
* shift + down arrow: id, down.
+
* {{key|Shift|Down}}: move to the line below within the prompt.
  
 
== Selecting and executing text from the screen ==
 
== Selecting and executing text from the screen ==
Line 27: Line 87:
 
= Using the script editor =
 
= Using the script editor =
  
You can create, edit and run scripts using Fiji's script editor. For details, please see [[Using the Script Editor|the Script Editor documentation]].
+
You can create, edit and run scripts using the built-in [[Script Editor]]. For details, please see [[Using the Script Editor|the Script Editor documentation]].
 +
 
 +
= Adding scripts to the Plugins menu =
 +
 
 +
For the script to appear in the ImageJ menus, the following must apply:
 +
 
 +
{{Box
 +
| text = ".txt" is not a supported script extension
 +
| width = 30%
 +
| float = right
 +
}}
 +
# The script file is saved in the <code>ImageJ.app/scripts</code> or the <code>ImageJ.app/plugins/Scripts</code> directory (or a subdirectory thereof).
 +
# The script name ends in a supported script extension. For example
 +
#* ".groovy" for groovy,
 +
#* ".js" for javascript,
 +
#* ".py" for jython,
 +
#* ".rb" for jruby,
 +
#* ".clj" for clojure,
 +
#* ".bsh" for beanshell, and
 +
#* ".ijm" for ImageJ 1.x macros.
 +
# The script name contains a '_' (underscore) character, e.g. "MyScript_.ijm".
 +
 
 +
{{Fiji | Replace <code>ImageJ.app</code> with <code>Fiji.app</code>}}
 +
 
 +
The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.
  
= Creating scripts and using "refresh scripts" =
+
Scripts in the top-level <code>ImageJ.app/plugins</code> directory will appear at the bottom of the ''Plugins'' menu. Scripts can be placed in other menus by nesting subdirectories, for example placing a script in the <code>ImageJ.app/scripts/File</code> directory will add it to the ''File'' menu.
  
On startup, ImageJ will run all "refresh scripts" plugins, one for each supported language. This will result in all scripts present within the plugins folders to be added to the menus.
+
If you aren't able to find your script, you can always run the [[Using_the_Command_Launcher|Command Finder]] to verify its location (or absence).
  
To run a script, just select it from the plugins menus.
+
Commands added to the menu in the described way can be called from other scripts. Use the [[macro recorder]] to get the required code for doing so.
  
If you edit a script that is already placed in the menus, you don't need to do anything else: just save the text file and run it again by selecting it from the menus.
+
== Adding JAR-packaged scripts to the menu ==
  
If you add a new script and ImageJ is running, just go to Plugins - Scripting and run the appropriate Refresh * Scripts for the language.
+
Scripts can be packaged in a JAR file for easier distribution to your colleagues and via [Update Sites]. For this purpose, [https://github.com/imagej/example-script-collection example-script-collection] can be used as the template Maven project.  
  
For the script to appear in the Plugin menus, it needs to terminate in the appropriate file extension. For example, ".js" for javascript, ".py" for jython, ".rb" for jruby, ".clj" for clojure, and ".bs" for beanshell script. The script must also contain a '_' (underscore) in the name.  The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.
+
Inside the example-script-collection jar, the scripts are in <code>./resources/scripts.</code>  and therefore get added to the menu when the JAR is on the classpath (i.e. in <code>./plugins/</code> or <code>./jars/</code>).
  
== Running Scripts in Headless Mode ==
+
ImageJ2 (and therefore Fiji) looks for scripts in subfolders of <code>./scripts/</code> as it is already described in the previous section, and for jars in <code>./jars/</code>. ImageJ1 recognizes plugins and scripts in <code>./plugins/</code>
  
If you want to run scripts in headless mode (i.e. without the GUI, by invoking fiji with the --headless option) then you have to make sure you run the appropriate "Refresh ... Scripts" plugin first.  Also, you need to append -batch to the command.  For example, if you have a script called test_ruby.rb, which appears in the Plugins menu somewhere as "test ruby", then you can run that with:
+
= Running scripts in headless mode =
  
  fiji --headless -eval 'run("Refresh JRuby Scripts",""); run("test ruby","");' -batch
+
See the [[Scripting Headless]] page for instructions on executing scripts headlessly.
  
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Latest revision as of 06:56, 20 July 2018

Learn
Topics
Introduction
Getting Started
User Guides
Tutorials
Tips and Tricks
Presentations
Plugins
Techniques
All Techniques
Colocalization
Deconvolution
Registration
Segmentation
Stitching
Tracking
Visualization
Scripting
Overview
Parameters
Basics of script writing
Batch processing
Script Editor
Auto Imports
Templates
Running headlessly
Comparisons
Toolbox
Multithreading in Clojure
Multithreading in JavaScript
Chess in Jython
Languages
BeanShell
Groovy
ImageJ Macro
JavaScript
Lisp (Clojure)
MATLAB
Python (Jython)
R (Renjin)
Ruby (JRuby)
Scala

ImageJ allows you to write scripts in several different languages.

Getting started

  • Press the [ key to open the Script Editor (or Shift-[ to open the Script Interpreter).
  • Optionally, choose a template from the Templates menu to get you started.
  • Otherwise, choose your language from the Language menu.
  • Grab code snippets for common tasks from the Scripting toolbox.
  • See Scripting comparisons for a side-by-side comparison of scripting languages.
  • See Category:Scripting for a list of all scripting-related pages on this wiki.

Supported languages

ImageJ's Script Editor supports many different languages. The following table summarizes the possibilities.

Recommended options
Groovy Groovy is a flexible and powerful scripting language, Java-like but less verbose and dynamically typed. Learn this, and using Java later (if needed) will become easier.
ImageJ Macro The ImageJ 1.x macro language is less powerful than the other scripting languages, but is designed to be easy to learn and use.
Python (Jython) Python is a popular choice among scientists.
JavaScript JavaScript is a popular choice among web developers.
Ruby (JRuby) Ruby is another popular choice among web developers.
Lisp (Clojure) Lisp is a popular choice among computer scientists.
R (Renjin) R is a popular choice among scientists and statisticians.
Other options
Java You can code Java plugins in the Script Editor. This is the most difficult path, but also the most powerful.
MATLAB ImageJ can interface bidirectionally with MATLAB. See the MATLAB Scripting page for details.
BeanShell BeanShell is an old script language, maintained mostly for backwards compatibility. It is nearly 100% compatible with Java syntax, but so is Groovy.
Scala Scala support is currently experimental, and has bugs.

Script parameters

There is a universal @parameter notation available across all scripts for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x GenericDialog because it is totally agnostic to the user interface, allowing such scripts to run in a variety of contexts.

See the script parameters page for details.

Using an interpreter

All scripting languages use the same basic interpreter, with the following common features.

General key bindings

  • : bring the previously typed command.
  • : bring the next typed command.
  • Enter or Return: execute the contents of the prompt.

Multiline editing and keybindings

You can enlarge the prompt by dragging the middle bar.

  • Shift+ Enter: create a new line within the prompt.
  • Shift+: move to the line above within the prompt.
  • Shift+: move to the line below within the prompt.

Selecting and executing text from the screen

On selecting text, a popup offers to:

  • copy
  • execute
  • save to a new file

Using the script editor

You can create, edit and run scripts using the built-in Script Editor. For details, please see the Script Editor documentation.

Adding scripts to the Plugins menu

For the script to appear in the ImageJ menus, the following must apply:

".txt" is not a supported script extension
  1. The script file is saved in the ImageJ.app/scripts or the ImageJ.app/plugins/Scripts directory (or a subdirectory thereof).
  2. The script name ends in a supported script extension. For example
    • ".groovy" for groovy,
    • ".js" for javascript,
    • ".py" for jython,
    • ".rb" for jruby,
    • ".clj" for clojure,
    • ".bsh" for beanshell, and
    • ".ijm" for ImageJ 1.x macros.
  3. The script name contains a '_' (underscore) character, e.g. "MyScript_.ijm".
Fiji-icon.png
Replace ImageJ.app with Fiji.app


The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.

Scripts in the top-level ImageJ.app/plugins directory will appear at the bottom of the Plugins menu. Scripts can be placed in other menus by nesting subdirectories, for example placing a script in the ImageJ.app/scripts/File directory will add it to the File menu.

If you aren't able to find your script, you can always run the Command Finder to verify its location (or absence).

Commands added to the menu in the described way can be called from other scripts. Use the macro recorder to get the required code for doing so.

Adding JAR-packaged scripts to the menu

Scripts can be packaged in a JAR file for easier distribution to your colleagues and via [Update Sites]. For this purpose, example-script-collection can be used as the template Maven project.

Inside the example-script-collection jar, the scripts are in ./resources/scripts. and therefore get added to the menu when the JAR is on the classpath (i.e. in ./plugins/ or ./jars/).

ImageJ2 (and therefore Fiji) looks for scripts in subfolders of ./scripts/ as it is already described in the previous section, and for jars in ./jars/. ImageJ1 recognizes plugins and scripts in ./plugins/

Running scripts in headless mode

See the Scripting Headless page for instructions on executing scripts headlessly.