You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _pages/scripting/groovy/index.md
+189Lines changed: 189 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,3 +26,192 @@ The aim of this page is not to teach how to program in Groovy. This purpose is m
26
26
### Use an IDE for Groovy scripting
27
27
28
28
As Groovy builds upon [Java](/develop/plugins), it can be use in a full fledged IDE with Fiji. If interested, follow this [tutorial](/scripting/groovy/ides).
29
+
30
+
## When to use Groovy
31
+
32
+
The following list will help you to decide if Groovy is the right choice for you to create scripts for ImageJ:
33
+
34
+
* If you have experience with Java, you can easily use Groovy for ImageJ scripting.
35
+
36
+
* If you want to be able to rapidly prototype something and make calls to external libraries, Groovy is a good choice.
37
+
38
+
* If you don't have experience with Java, but would like to get some, then scripting with Groovy can be a good way to learn.
39
+
40
+
* If you have very little or no experience in programming, you may like to explore [Jython](/scripting/jython) as it is an easy-to-read but feature-rich language.
41
+
42
+
43
+
## Explanation
44
+
At a basic level, Groovy is very similar to Java. As your familiarity with it grows, you will find it can do some things that Java can't.
45
+
Like Java, Groovy can access any class libraries that are present in the classpath. This allows you to to include 3rd party libraries which may not immediately be present in Fiji/ImageJ, but which you can download. Examples would include database connections, libraries for communication purposes, The list is long!
46
+
47
+
## Groovy basics for ImageJ
48
+
49
+
{% include notice icon="info" content='For an introduction in ImageJ scripting visit the page [Scripting basics](/scripting/basics).' %}
50
+
51
+
### Hello, World!
52
+
53
+
#### - With print / println
54
+
55
+
The print and println commands send output to the console, with the difference being that println always appends a newline character at the end
56
+
57
+
```groovy
58
+
println "Hello, World!"
59
+
// Let's show it handling numbers too
60
+
println "Result of 2 + 2: " + (2+2)
61
+
62
+
// what happens if we don't use the parentheses?
63
+
print "Result of 2 + 2: " + 2+2
64
+
```
65
+
66
+
Note - `print` and `println` will send their output to the standalone console if it's open. If not, it will go to the console of the Fiji Script Editor. Use cases would be where you want some sort of text output (to show values, progress etc), but you don't want it popping up for the user.
67
+
68
+
#### - With IJ.log()
69
+
70
+
`IJ.log()` is an example of an ImageJ java function (also called a method.
71
+
It creates a window in ImageJ (if one isn't already open) and writes text to it.
72
+
Newline characters are always appended with each call.
73
+
74
+
```groovy
75
+
import ij.IJ
76
+
77
+
IJ.log("Hello, World!")
78
+
// Let's show it handling numbers too
79
+
IJ.log("Result of 2 + 2: " + (2+2))
80
+
81
+
// what happens if we don't use the parentheses?
82
+
IJ.log("Result of 2 + 2: " + 2+2)
83
+
```
84
+
85
+
If you tried out both sets of examples, the lines containing (2+2) will have evaluated as `4`, whereas the lines that don't have the brackets are treated as strings, giving `22`
86
+
87
+
Image selection using the GenericDialog class
88
+
89
+
This example script will create up to 10 new images and create a GenericDialog to select 3 of them. Finally the names of the selected images are printed to the Log window. It is recommended to copy the code to the [Script Editor](/scripting/script-editor) and run it by yourself.
90
+
91
+
```groovy
92
+
// Import the classes that are needed.
93
+
94
+
import ij.IJ
95
+
import ij.WindowManager
96
+
import ij.gui.GenericDialog
97
+
import ij.plugin.frame.RoiManager
98
+
99
+
// The IJ class contains a number of utilities. For this script, it provides the "log" functionality
100
+
// A class which gives access to the window objects
101
+
// A class which allows for creation of custom dialogs with relative ease.
102
+
// The ROI Manager - useful for accessing ROIs.
103
+
104
+
// next we'll define some functions.
105
+
106
+
// Function to create a test image
107
+
def createTestImage() {
108
+
int imageWidth = 512 // here, we're using explicit types - int holds an integer.
109
+
int imageHeight = 512
110
+
int boxWidth = 128
111
+
int boxHeight = 128
112
+
int offsetX = (int) 192 * 2 * Math.random() // (int) causes the rest of the statement to be forced to an integer - no decimal places!
113
+
int offsetY = (int) 192 * 2 * Math.random()
114
+
int counts = 64
115
+
int stdv = 16
116
+
117
+
// The following are nested definitions. They are not available outside the "createTestImage" function.
118
+
// the following line is called a closure. It's a short-hand way of creating a function.
119
+
// This one returns a string: makeTitle("Testing", 1, 2) will give 'Testing: 1, 2' as the output.
0 commit comments