Open one or more image files from macro function

Dear all,
I want to open many files in Imagej via a a custom macro which will be placed into the the
toolsets folder.

Later on i want to trigger additional events like image calibration withtin this marco. This is also the reason why i do not want to use the stardard open File dialog. I have enabled “Use jFileChooser to open/save” in Edit/Options/Input-Output, such that my default Filedialog aloows me to open multiple images at once.

What i want to achive is this:

  • i would like to only show image files with a specific ending ( by default this should be .tif) and nothing else like txt files or other non image data!
  • all selected images should be opened
  • When an image is closed it should be saved to a new folder with all processing/ annotations etc that have been done to it.

Here is my code:

macro "Calibrate SEM Action Tool - C000 T5e13A"{

// OpenDialog Demo
//
// This macro demonstrates how do use the 
// File.openDialog() macro function.
// unfortunately this dialoge allows me to only load one file at a time 
  path = File.openDialog("Select a File");
  //open(path); // open the file
  dir = File.getParent(path);
  name = File.getName(path);
  print("Path:", path);
  print("Name:", name);
  print("Directory:", dir);
  list = getFileList(dir);
  print("Directory contains "+list.length+" files");
 open(path); 
 // Here i want to call additional macors to calibrate the image etc
}

Unfortunately this opens only a single file at a time. Furthermore i would also have the option to simply load all files inside a folder with the selected ending.

All help is really appreciated

@NorrinRadd

If I were you… I’d modify the following code example provided via the Script Editor (Templates > ImageJ 1.x > Batch > Process Folder (IJ1 Macro)):

/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
        // THIS IS WHERE YOU CAN OPEN YOUR FILES, ETC AS YOU WANT
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}

You can modify this code easily to do what you need… it already will only open .tif files at the moment (see suffix parameter) and by using Script Parameters - it will make your life way easier! :wink:

2 Likes

Thank you,

That worked pretty well for batch processing. I would still like to have a way to modify my code to have a gui where i can chosse which files i would like to calibrate.

Is this possible?

Furthermore,

Once i try to encapsulate my code into a toolset/action tool i get the follwoing error message:

“Statement can not begin with ‘’#’ in line70”

<#>@ File (label = “Input directory”, style = “directory”) input

Can i somehow turn this script into a clickable icon in the toolsets row?

@NorrinRadd

I’m not sure what you mean by this. Any script can be saved and made searchable (via Search Bar) if you (on a Mac) Show Package Contents on Fiji icon… Make a folder called “Plugins” and save in the folder called ‘scripts’… use an underscore in the filename (.ijm file)… if you close/reopen Fiji - you’ll see it in the Plugins dropdown menu.

And for the error - it’s hard to tell what exactly is going on without being able to see/run your code. All Script Parameters are usually declared at the very top of a script - so should be on the first lines… Perhaps you can create a small working code example to demonstrate this error so we can try to run and test/fix it?

If i put this into the toolset folder chosse this toolset and click on the icon i get the above error:

macro "Calibrate SEM in Folder Action Tool  -C000 T5e13A"{
/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
//#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix


output = input + "/Calibrated_tifs/"
File.makeDirectory(output);


// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		// switch off recursion
//		if(File.isDirectory(input + File.separator + list[i]))
//			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {

// here goes the code for calibration ( not shown for brevity)
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}
}

@NorrinRadd

You don’t need to encapsulate this code. The script is fine as-is to save and call via the Plugins menu as I described. So just delete the
macro "Calibrate SEM in Folder Action Tool -C000 T5e13A"{
} and you should be ok. Too - are you trying to comment out the output Script Parameter? It might still be seen using that syntax - so delete the line (#@ File (label = "Output directory", style = "directory") output) completely if you don’t want to declare an output directory.

Thank you for your reply! I wanted to have this code available in the custom toolset ribbon. This is why i asked. Is this not possible?

Thank you in advance!

@NorrinRadd

So - sorry! I didn’t know this until now (thank you @ctrueden) - you cannot use Script Parameters in macro toolset commands. Boo! It’s not supported - as you found out.

The easiest workaround is saving the scripts as I told you… then you can create a one-line macro (using toolset thing) that calls that script. Give that a go!

IF i try to encapssulate the scritpt like this:

macro  "Calibrate SEM in Folder Action Tool  -C000 T5e13A"{
	// Run custom macro
	   runMacro("Full path to my script")
}

i get the identical error message as before? Any tips on what i do wrong?

Thank you in advance

Any thoughts on this one @ctrueden - because I myself do not know the answer here…

The runMacro macro function does not support script parameters. Instead of using runMacro("Full path to my script"), try the following:

  1. Install your macro into Fiji.app/scripts/Plugins/Custom/My_Macro.ijm, so that it appears in the menus at Plugins › Custom › My Macro (substitute whichever menu path and filename you want).
  2. Use the Macro Recorder to record an execution of the macro from the menus. It should produce a line of code that invokes the run(...) function, like:
    run("My Macro", "");
    
  3. Paste this line of code into your custom tool block.
3 Likes

Thanks a bunch! That worked like a charm!
Awesome community!

Thank you for the quick help!

2 Likes