How to automate direcrory selection

Hi all,

I am trying to create a pipeline that will automatically process multiple images. The problem is that each time I start the process and load a new image, Fiji asking me to enter the input and output directories. Is there a way to change the following macro such that the program doesn’t prompt me to “choose the directory”?

I’d be very thankful to nay suggestions.

Thanks!

//select input directory for pipeline 

dir1 = getDirectory("C:\Boss\Platelet-distances\2_Input");
list = getFileList( dir1 );
for ( i=0; i<list.length; i++ ) {
    open( dir1 + list[i] );

Run("...")

dir2 = getDirectory("C:\Boss\Platelet-distances\3_Output"); 
ids=newArray(nImages); 
for (i=0;i<nImages;i++) { 
        selectImage(i+1); 
        title = getTitle; 
        print(title); 
        ids[i]=getImageID; 

        saveAs("tiff", dir2+title); 
} 
run("Close All");

If you do not want to select a directory simply write

dir1 = "C:\\Boss\\Platelet-distances\\2_Input";

instead of

dir1 = getDirectory("C:\Boss\Platelet-distances\2_Input");

On
https://imagej.nih.gov/ij/developer/macro/functions.html
you can find information about the getDirectory function:

getDirectory(string)
Displays a “choose directory” dialog and returns the selected directory, or returns the path to a specified directory, such as “plugins”, “home”, etc. …

1 Like

hmm, I tried removing "getDirectory (……) and it didn’t work…

@varlamov
@phaub
I work under windows
@phaub 's proposal works well.

dir1 = "C:\\Boss\\Platelet-distances\\2_Input";

list = getFileList( dir1 );
for ( i=0; i<list.length; i++ ) {
    open( dir1 + list[i] );
}

with double anti slach
or

dir1 = "C:/Boss/Platelet-distances/2_Input";

list = getFileList( dir1 );
for ( i=0; i<list.length; i++ ) {
    open( dir1 + list[i] );
}

I did try and now I am getting this error massage

Hi @varlamov!

I think you are missing the double slash at the end of dir1

dir1 = "C:\\Oleg\\Platelet-distances\\2_Input\\"

so that when you concatenate this directory path with the filename, you actually get a correct complete path to the file.

Cheers,
Nico

@NicoDF
You are right!
And it was I who introduced the error in my answer!!!

1 Like

Hi Nico, you are a genius! The pipeline works very well now. Thank you very much.
Oleg

I have an additional question, which sounds like an easy task for a programmer.

Is it possible to load a new image from a parent directory after each run? Essentially, I envision the following steps:

  1. Move file-1 from the parent directory to input directory.
  2. Run image analysis macro.
  3. Clear input directory.
  4. Move file-2 from parent directory to input directory.
  5. Repeat until parental directory in empty.

This should create high-throughput automation.
Thanks!

Have a look at the File Functions in the macro built-in functions: https://imagej.nih.gov/ij/developer/macro/functions.html

File.copy(path1, path2);
File.delete(path);

I just wonder why you would need these operations that you describe in the first place?
Why not work on the parental directory directly? From you description the moving serves no direct purpose.

1 Like