Commenting in Groovy

Short and sweet - any characters preceded by // will be ignored by the script compiler - this allows you to place notes to yourself or users of your code. A month or year from now you may not remember why you did something, so comment early, comment often, and comment more clearly than I usually do.

 
//This is a comment on a line /* The comment in this format starts here but keeps going on each subsequent line until we reach */ This is no longer a comment and will throw an error.

One further point unrelated to commenting - any code you copy from other sources, especially the forum, that is not formatted appropriately will throw an error if there is a quotation mark in it. 

Copying comments

Line 1 was copied from an unformatted forum post, while line 2 was typed into the scripting window..

1 will throw an error and fail, 2 works and you can see that the string is correctly formatted (green)

If you do post on the forum, help us help you! Format your code appropriately!

Formatting forum posts

The first line was not formatted correctly, and will result in errors if copied into QuPath.

The second getPathClass() is formatted correctly, as seen by the background change and colored string on the right.

Using the preformatted text option as shown second, will be QuPath friendly.

One further option is using dialog popups, as shown in the code taken from a simple example here. These are great when you really want to get a user's attention, but may be less useful for scripts intended to be run "For Project."

def obj = getSelectedObject() if (obj == null) { Dialogs.showErrorMessage("Selection", "Select an annotation first!") return } removeObjects(obj.getChildObjects(), false)
A popup is created when no object is selected, as shown. If an object is selected, all child objects of that object are deleted.

A popup is created when no object is selected, as shown. If an object is selected, all child objects of that object are deleted.