Difference between revisions of "Script Templates"

m
(Adding A New Template: clarify that the language name needs to be the sub-directory name)
Line 9: Line 9:
 
The [[Script Editor]] will automatically search the <code>script-templates</code> directory and register any discovered scripts as templates. For example, a [[Maven|Mavenized]] project could add a sample script in <code>src/main/resources/script-templates</code> and it would be packaged into the appropriate location of the resultant <code>.jar</code>.
 
The [[Script Editor]] will automatically search the <code>script-templates</code> directory and register any discovered scripts as templates. For example, a [[Maven|Mavenized]] project could add a sample script in <code>src/main/resources/script-templates</code> and it would be packaged into the appropriate location of the resultant <code>.jar</code>.
  
Script language determination is based simply off file extension. So a <code>FFT.rb</code> script would be indexed under the <code>Templates > Ruby</code> menu.
+
The templates need to be put into a subdirectory corresponding with the correct  script language name. So a <code>FFT.rb</code> script would need to be but into the `script-templates/Ruby/` directory to be available under the <code>Templates > Ruby</code> menu. This is required so that templates are made available only when the corresponding script language is available.  
  
For examples of how the existing templates are structured and distributed, take a look at the core [https://github.com/scijava?query=scripting SciJava scripting projects].
+
For examples of how the existing templates are structured and distributed, take a look at the [https://github.com/imagej/imagej-legacy/tree/master/src/main/resources/script-templates <code>imagej-legacy</code> project] (which maintains the script templates that were previously shipped with Fiji's script editor).
  
 
= Template Best Practices =
 
= Template Best Practices =

Revision as of 15:48, 20 October 2014

Template:Scripting

Templates are pre-written scripts in a particular scripting language. They can be distributed as any other plugin, and automatically discovered by the Script Editor. Templates thus present an excellent way to demonstrate a particular operation to a wide audience of users.

Available templates are sorted by language under the Templates menu of the Script Editor. The contents of a template will be loaded into the editor window when selected. The script can then be inspected, executed and modified as normal.

Adding A New Template

The Script Editor will automatically search the script-templates directory and register any discovered scripts as templates. For example, a Mavenized project could add a sample script in src/main/resources/script-templates and it would be packaged into the appropriate location of the resultant .jar.

The templates need to be put into a subdirectory corresponding with the correct script language name. So a FFT.rb script would need to be but into the `script-templates/Ruby/` directory to be available under the Templates > Ruby menu. This is required so that templates are made available only when the corresponding script language is available.

For examples of how the existing templates are structured and distributed, take a look at the imagej-legacy project (which maintains the script templates that were previously shipped with Fiji's script editor).

Template Best Practices

Above all else, templates should be well documented. Templates are intended to explain specific functionality in a given language, to users who may have limited experience with the language or programming/scripting in general.

Ideally, a template will be focused on an a single objective (e.g. opening an image, calculating a threshold, operating on a directory, etc...).

It is especially helpful to document how the template can be adapted to meet the user's needs.