所有语言脚本都可以使用通用的 #@parameter 符号来声明输入和输出。这种方法原始的 ImageJ 的 GenericDialog,因为它完全不依赖于用户界面,允许此类脚本在各种上下文中运行。与ImageJ2 plugins一样,脚本参数化基于SciJava parameter annotation——因此编写的经验可以直接转化为脚本编写,反之亦然。
基本语法
#@参数使用规则如下:
- 参数声明以
#@开头。每行都包含一个参数声明或脚本指令,仅此而已。 #@ Type variableName将声明指定类型的输入,并指定指定名称。(鼓励在#@和Type之间使用空格,但不强制要求。)#@output Type outputName将指定名称的变量声明用于给定类型的输出参数。Type参数是可选的,因为默认情况下输出将被视为Object。(对于output指令和其他脚本指令,#@和指令之间不允许有空格。)
例如,如果我们查看斐济提供的Greeting.py template:
我们看到声明的类型为 String 的输入参数 name。 @Parameters由框架自动处理;如果在用户界面可用时运行此脚本(例如从脚本编辑器),则 name 我们的参数将通过对话框对话框自动获取:

由于@parameters的一般性质,我们还可以运行此脚本headlessly。
脚本完成后,任何#@output变量都由框架根据其类型进行处理。在本例中,我们期望打印greeting变量,因为它是string。
参数类型
下面提供了可能的数据类型和相应的小部件的列表。
任选的样式参数定义了小部件在输入窗口中的装载方式。
请参阅预览样式中的相应小部件部分。
| 数据类型 | 小部件类型 | ** 可用款式** |
|---|---|---|
boolean Boolean |
布局 | |
byte Byte short Short int Integer long Long |
数字字段 | slider spinner scroll bar |
Float Double |
数字字段 | slider spinner scroll bar |
BigInteger BigDecimal |
数字字段 | §§§20§§§ §§§21§§§ §§§22§§§ |
| §§§23§§§ §§§24§§§ §§§25§§§ | 文本字段 | §§§26§§§ §§§27§§§ §§§28§§§ |
| §§§29§§§§§§30§§§ | (>=2张图片) 触发下拉列表 | |
| §§§31§§§ | 颜色选择器 | |
| §§§32§§§ | 日期选择器 | |
| §§§33§§§ | 文件选择器 | §§§34§§§ §§§35§§§ §§§36§§§ §§§37§§§ §§§38§§§ |
注意事项和陷阱:
float也是敏感的字段,但与Float相比,小数字点t不会显示在表单中(注意大写F)。- 当在代码中默认设置值并在表单中输入时,相关的 issue 与
int和double一起出现,在接下来的运行时无法正确调用该值。请改用Integer和Double。 - 单个
#@ ImagePlus或#@ Dataset字段不会出现在输入表单中,而是自动处理当前图像。但是,如果存在两个#@ ImagePlus(或分别为#@ Dataset),则它们将呈现为下拉按钮。 通过实施InputWidget,可以扩展此列表。
示例
###整数和小数输入
整数和浮点数可以具有可选参数 min、max 和 stepSize 值(默认为 1)以及由 value 指示的默认值。
不同的风格也可能是的。
#@ Integer (label="Default integer style", min=0, max=10, value=5) myint1
#@ Integer (label="Slider integer style", style="slider", min=0, max=10, stepSize=2) myint2
#@ Float (label="Slider with float", style="slider", min=0, max=1, stepSize=0.1) myfloat

还可以指定具有固定小数量附件的格式…
// Specified format
#@ Double (value=0.0123, persist=false, style="format:#.##") i
#@ Double (value=0.0123, persist=false, style="format:#.00") j
#@ Double (value=123.45, persist=false, style="format:#####.#####") k
#@ Double (value=123.45, persist=false, style="format:00000.00000") l
…与倾斜或滚动条结合使用:
// Sliders and scroll bars
#@ Double (value=1, min=0, max=10, stepSize=0.001, persist=false, style=slider) m
#@ Double (value=1, min=0, max=10, stepSize=0.001, persist=false, style="slider,format:0.0000") n
#@ Double (value=1, min=0, max=10, stepSize=0.001, persist=false, style="scroll bar") o
#@ Double (value=1, min=0, max=10, stepSize=0.001, persist=false, style="scroll bar,format:0.0000") p
科学记数法可以用 format:0.#####E0指定
参数属性
如果您查看 @Parameter annotation,您会发现它有许多属性,例如,name 和 description。
脚本参数可以按照以下准则设置这些属性:
- 所有属性均紧随
#@type声明之后的 单引号表述 中定义。 - 属性由comma-separated list of key=value pairs设置
属性是您的自定义框架应如何处理 #@parameter 的方式。
小部件标签
小部件是向用户显示的用于收集输入信息的用户界面元素。例如,我们可以将自定义标签添加到 Greeting.py 脚本的字段中,而不仅仅是向用户显示“名称”,如下所示:
#@ String (label="Please enter your name") name
#@ output String greeting
greeting = "Hello, " + name + "!"
小部件鼠标悬停
我们可以为我们的字段添加 description 属性来提供鼠标悬停文本:
#@ String (label="Please enter your name", description="Your name") name
#@ output String greeting
greeting = "Hello, " + name + "!"
###默认值
还支持默认值作为参数属性:
#@ Integer (label="An integer!",value=15) someInt`
坚持
默认情况下,参数值在脚本运行之间保留。这意味着先前运行的参数值将初始初始值。请注意,持久值将覆盖定义的default value。
#@ Integer (label="An integer!", value=15, persist=false) someInt`
Currently, “two scripts which declare the same parameter name, even with different types, will stomp each other.” See 1.
可见性
该属性设置是否应显示、可编辑和/或记录参数。
- NORMAL:根据数据来源的目的,参数包含在历史记录中,并且在记录脚本时作为参数包含在内。
- TRANSIENT:出于数据来源的目的,参数被排除在历史记录之外,但在脚本脚本时仍作为参数包含在内。
- INVISIBLE:根据数据来源的目的,参数来自历史记录中的修正,并且在记录脚本时也作为参数修正。此最终选项只能用于对输出没有影响的参数,例如“详细”标志。
- MESSAGE:参数值仅作为消息,用户不可编辑,也不可作为输入或输出参数包含在内。选项required应设置为 false。

#@ String (visibility=MESSAGE, value="This is a documentation line", required=false) msg
#@ Integer (label="Some integer parameter") my_int
您可以use HTML来格式化消息字符串,例如:
#@ String (visibility=MESSAGE, value="<html>Message line 1<br/>Message line 2<p>Let's make a list<ul><li>item a</li><li>item b</li></ul></html>") docmsg
#@ Integer anIntParam

Currently if a script containing a MESSAGE string is recorded with the macro recorder and the resulting recorded code executed, a window will show up containing only the MESSAGE string This is unexpected and will be corrected in the future.
作品选择
通过添加 choices={...} 属性,任何参数都可以变成多选选择器。
选择小组件可以具有不同的样式,例如下拉列表或单选按钮。
#@ String (choices={"Option 1", "Option 2"}, style="listBox") myChoice123
#@ String (choices={"Option A", "Option B"}, style="radioButtonHorizontal") myChoiceABC
print(myChoice123)
print(myChoiceABC)

文件和文件夹
默认情况下,#@ File配置单个文件创建选择器。下面是 python 中的一个例子:
#@ File (label="Select a file") myFile
print(myFile)
您也可以请求多个文件或文件夹。但是,多个文件/文件夹输入尚不能进行宏录制。
ImageJ 宏语言示例:
#@ File[] listOfPaths (label="select files or folders", style="both")
print("There are "+listOfPaths.length+" paths selected.");
for (i=0;i<listOfPaths.length;i++) {
myFile=listOfPaths[i];
if (File.exists(myFile)) {
print(myFile + " exists.");
if (File.isDirectory(myFile)) {
print("Is a directory");
} else {
print("Is a file");
}
}
}
如果您想专门选择文件或文件夹,请使用 style 属性:
#@ File (label="Select a file", style="file") myFile
#@ File (label="Select a directory", style="directory") myDir
print(myFile)
print(myDir)
单个File参数支持样式“文件”、“目录”、“打开”、“保存”。
对于多个文件或目录,样式为复数
#@ File[] (label="Select some files", style="files") listfiles
#@ File[] (label="Select some directories", style="directories") listdirs
print(listfiles)
print(listdirs)
File[]参数支持“文件”、“目录”、“双向”样式。
风格
您可以影响某些输入小部件的视觉样式。请参阅上一段以获取特定于小部件的样式示例。