Can you import ij in macro scripting?

We are trying to see which images have been modified, and then save them. Is there a way to import classes into the macro scripts, a better way to do this, or any documentation you can send us a link to?

function checkForChanges(){
	var imageIDs = ij.WindowManager.getIDList();
	for(i = 0; i < imagesIDs.length(); i++){
		var image = ij.WindowManager.getImage(imageIDs.get(i));
		if(image.changes){
			run("Bio-Formats Exporter", "save=C:\\Users\\Matthew\\Pictures\\" + i + ".tif export compression=Uncompressed");
		}
	}
}
checkForChanges();

The macro language is not a full scripting language; it does not have access to the entire Java API, but only a fixed set of macro functions.

To code against the Java API, you can use one of the available scripting languages. Given your syntax above, JavaScript might be a good choice—especially since it can be recorded with the Macro Recorder.

1 Like

Thanks! We are actually developing in python but this needed to be done in the macro language, and just couldn’t find much documentation on it. From the set of macro functions you provided a link to we were able to find equivalent functions to get it working.

2 Likes

May I encourage you to go ahead and post your solution code here, so other people can benefit from this forum (just as you did)?

1 Like
// FILEPATH: path to directory to save the images in
function saveChanges(){
    // Checks if an image is open
    if(nImages != 0){
        // Store the id of the open image to reselect it once the process is over
        selectedImage = getImageID();

        // Checks every image
        for (i=0; i < nImages; i++){ 
            
            // Select image
            selectImage(i+1);

            // Replace spaces with underscore, not necessary could leave as spaces
            title = replace(getTitle(), " ", "_");

            // If anything exists after the extension, move it to the front of the image name instead
            if(indexOf(title, "_", indexOf(title, ".")) != -1){
                title = substring(title, indexOf(title, "_", indexOf(title, ".")) + 1) + substring(title, 0, indexOf(title, "_", indexOf(title, ".")));
            }

            // If file doesn't exist, save it
            if(File.exists("FILEPATH\\\\" + title) != 1){
                run("Bio-Formats Exporter", "save=[FILEPATH\\\\" + title + "]" + " export compression=Uncompressed");
            }

            // If changes have been made to the image, save it
            if(is("changes")){
                
                // Name without extension
                name = substring(title, 0, indexOf(title,"."));

                // Counter for file name
                titleIteration = 0;

                // File extension
                ext = substring(title, indexOf(title, "."));

                // While the file exists, increment the counter to produce a different name
                while(File.exists("FILEPATH\\\\" + name + "(" + titleIteration + ")" + ext) == 1){
                    titleIteration = titleIteration + 1;
                }

                // Name of file to export
                title = name + "(" + titleIteration + ")" + ext;

                // Export the file
                run("Bio-Formats Exporter", "save=[FILEPATH\\\\" + title + "]" + " export compression=Uncompressed");

                // Rename the open window to the new file name
                rename(title);

                // Mark file has no changes
                setOption("Changes", false);
            }
        }
        //Select the image that was originally selected
        selectImage(selectedImage);
    }
}
2 Likes