Anatomy of “Hello World!”
Let’s see more details of what the single line code we wrote is doing.
print() is a build-in macro function that requests ImageJ to take the content within the parenthesis and print that out in the "Log" window. This content, which we genearlly call the argument of the function, is an input value given to the function. The output of this function is the printed text in the Log window. Note that when a text is given as an argument, it must be surrounded by double quotes ("").
Where do we get information as such for other macro functions? The best reference for ImageJ macro functions is in the ImageJ web site
For example, you could find definition of print("") function on the web site as quoted below:
Outputs a string to the "Log" window. Numeric arguments are automatically converted to strings.
The print() function accepts multiple arguments. For example, you can use print(x,y,width, height)
instead of print(x+" "+y+" "+width+" "+height).
If the first argument is a file handle returned by File.open(path),
then the second is saved in the referred file (see SaveTextFileDemo).
Numeric expressions are automatically converted to strings using four decimal places,
or use the d2s function to specify the decimal places.
For example, print(2/3) outputs "0.6667" but print(d2s(2/3,1)) outputs "0.7".
As print can do many things, its explanation is extraordinary long, but by carefully reading it, you will save time afterwards by the knowledge of wide spectrum of things that the print function can do e.g. directly save text as a file.
Macro can be saved as a file.
In the editor, do [File -> Save]. Just save the file wherever you want in your file system. When you want to use the macro again, load the macro by [File > Open].
Add another line "print("\\Clear");"before the first line (below, code 1.51. don’t forget the semi-colon at the end!).
Then test also another macro when you put the same line after "Hello World!".
What happened? Any difference in the behavior?
Answer: The first code prints "Hello World!", while the second code prints nothing. This is becaise print("\\Clear") is a command that clears the Log window. In the first code, “Hello World” is printed after the window clearing, and in the second case the Log window is wiped out right after the printing of “Hello World”. Effectively it looks like nothing has happened.
Try modifying the third line in code 1.51
and check that the modified text will be printed in the "Log" window.