Skip to content, Skip to search

Changes

Scripting

799 bytes removed, 12:56, 20 July 2018
Adding JAR-packaged scripts to the menu: Small fixes
__FORCETOC__ {{ScriptingLearn|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.
= Supported languages =
ImageJ's [[Script Editor]] supports many different languages. The following table summarizes the following languages:possibilities.
* {| class="wikitable"| colspan=2 style="background: #ddd; text-align: center; font-weight: bold" | Recommended options|-! [[Beanshell Groovy Scripting|BeanShellGroovy]]* | [[wikipedia:Groovy Scripting(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)]]| [[Introduction into Developing Pluginswikipedia:Python (programming language)|JavaPython]]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.|-! [[Jython Renjin Scripting|Python R (JythonRenjin)]]* | [[JRuby Scripting|Ruby wikipedia:R (JRubyprogramming language)|R]]is a popular choice among scientists and statisticians.|-Additionally| 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, these languages but also the most powerful.|-! [[MATLAB Scripting|MATLAB]]| ImageJ can be installed via interface bidirectionally with MATLAB. See the [[MATLAB Scripting]] page for details.|-! [[Beanshell Scripting|BeanShell]]| [[httpwikipedia://fijiBeanShell|BeanShell]] is an old script language, maintained mostly for backwards compatibility.sc/Update_Sites update sitesIt is nearly 100% compatible with Java syntax, but so is [[Groovy]:].|-* ! [[MATLAB Scala Scripting|MATLABScala]]| [[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.
The rules See the [[script parameters]] page for <code>@parameter</code> use is as follows: <ol><li>All parameter declarations must appear in comments. Each comment line contains a single parameter declaration and nothing else.</li><li>Any parameters after the first non-parameter line will not be recognized.</li><li><code>@type variableName</code> will declare an input of the indicated type, assigned to the specified name.</li><li><code>@OUTPUT type outputName</code> will declare the variable of the specified name as an output parameter with the given type.</li></ol> For example, if we look at the Greetingdetails.py template supplied with Fiji:
<source lang="Python">
# @String name
# @OUTPUT String greeting
 
# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.
 
greeting = "Hello, " + name + "!"
</source>
 
We see that an input parameter <code>name</code> of type <code>String</code> is declared. This will automatically be harvested via a pop-up dialog when the script is run.
 
When the script is completed, we expect to have a <code>String</code> variable named <code>greeting</code> which will be displayed as appropriate, based on the variable type.
 
Note that if we added an extra comment to the top of our script as such:
 
<source lang="Python">
# A simple python script
# @String name
# @OUTPUT String greeting
 
# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.
 
greeting = "Hello, " + name + "!"
</source>
 
We would actually break the script, as the parameters would not be harvested or displayed due to violation of the second parameter rule.
= Using an interpreter =
== General key bindings ==
* {{Key|up arrow}}: bring the previously typed command.* {{Key|down arrow}}: bring the next typed command.* {{Key|enter }} or {{Key|return}}: execute the contents of the prompt.
== Multiline editing and keybidingskeybindings==
You can enlarge the prompt by dragging the middle bar.
* {{key|Shift}}+{{key||Enter}}: create a new line within the prompt.* {{key|Shift}}+{{key||Up}}: move to the line above within the prompt.* {{key|Shift}}+{{key|Down}}: id, downmove to the line below within the prompt.
== Selecting and executing text from the screen ==
= Using the script editor =
You can create, edit and run scripts using Fiji's script editorthe built-in [[Script Editor]]. For details, please see [[Using the Script Editor|the Script Editor documentation]]. = Script Parameters = There is a universal <code>@ parameter</code> notation available across all scripts for declaring inputs and outputs. The rules for their use is as follows: <ol><li>All parameter declarations must appear in comments. Each comment line contains a single parameter declaration and nothing else.</li><li>Any parameters after the first non-parameter line will not be recognized.</li><li><code>@type variableName</code> will declare an input of the indicated type, assigned to the specified name.</li><li><code>@OUTPUT type outputName</code> will declare the variable of the specified name as an output parameter with the given type.</li></ol> For example, if we look at the Greeting.py template supplied with Fiji: <source lang="Python"># @String name# @OUTPUT String greeting # A Jython script with parameters.# It is the duty of the scripting framework to harvest# the 'name' parameter from the user, and then display# the 'greeting' output parameter, based on its type. greeting = "Hello, " + name + "!"</source> We see that an input parameter <code>name</code> of type <code>String</code> is declared. This will automatically be harvested via a pop-up dialog when the script is run.
When the script is completed, we expect = Adding scripts to have a <code>String</code> variable named <code>greeting</code> which will be displayed as appropriate, based on the variable type.Plugins menu =
Note that if we added an extra comment For the script to appear in the ImageJ menus, the top of our script as suchfollowing must apply:
<source lang{{Box| text ="Python.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).# A simple python The scriptname ends in a supported script extension. For example# @String name* ".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.# @OUTPUT String greetingThe script name contains a '_' (underscore) character, e.g. "MyScript_.ijm".
# A Jython script {{Fiji | Replace <code>ImageJ.app</code> with parameters.# It is the duty of the scripting framework to harvest# the 'name' parameter from the user, and then display# the 'greeting' output parameter, based on its type<code>Fiji.app</code>}}
greeting = "Hello, " + name + "!"</source>The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus.
We would actually break 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, as in the parameters would not be harvested or displayed due <code>ImageJ.app/scripts/File</code> directory will add it to violation of the second parameter rule''File'' menu.
= Creating scripts and using "refresh scripts" =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).
On startup, ImageJ will run all "refresh Commands added to the menu in the described way can be called from other scripts" plugins, one for each supported language. This will result in all scripts present within Use the plugins folders to be added [[macro recorder]] to get the menusrequired code for doing so.
To run a script, just select it from == Adding JAR-packaged scripts to the plugins menus.menu ==
If you edit Scripts can be packaged in a script that is already placed in the menusJAR file for easier distribution to your colleagues and via [Update Sites]. For this purpose, you don't need to do anything else[https: just save the text file and run it again by selecting it from //github.com/imagej/example-script-collection example-script-collection] can be used as the menustemplate Maven project.
If you add a new Inside the example-script -collection jar, the scripts are in <code>./resources/scripts.</code> and ImageJ is running, just go therefore get added to Plugins - Scripting and run the appropriate Refresh * Scripts for menu when the JAR is on the languageclasspath (i.e. in <code>./plugins/</code> or <code>./jars/</code>).
For the script to appear ImageJ2 (and therefore Fiji) looks for scripts in the Plugin menus, subfolders of <code>./scripts/</code> as it needs to terminate is already described in the appropriate file extension. For exampleprevious section, ".js" and for javascript, ".py" for jython, "jars in <code>.rb" for jruby, "/jars/</code>.clj" for clojure, ImageJ1 recognizes plugins and ".bs" for beanshell script. The script must also contain a '_' (underscore) scripts in the name. The extension will be stripped and any underscores will be turned into spaces before the script is added to the menus<code>./plugins/</code>
== Running scripts in headless mode ==
See the [[Headless#Running_scripts_in_headless_mode|Scripting Headless]] page for instructions on executing scripts headlessly.
[[Category:Scripting]]
Emailconfirmed
25
edits