Dialog that can return both a folder or a file in ImageJ Javascript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Dialog that can return both a folder or a file in ImageJ Javascript

lechristophe
Hi,

(I asked this on the ImageJ forum but I figured it's more an IJ1 question
and some people may have an answer here)

I have javascripts that I need to duplicate to do the same operation either
on a single file or on all the files within a folder (batch). The code
could be easily merged in a single script except for the choice of the
source by the user:
- to choose a file I use ImageJ OpenDialog class
- to choose a folder I use ImageJ DIrectoryChooser class

Sadly, using OpenDialog I can't select a file (and I can't select a file
using DirectoryChooser). Is there a way to have a file choosing dialog that
would let the user select a file or a folder (selecting it and click OK for
a file and folder, or clicking OK without selection to choose the current
folder, similar to the system file and ImageJ open dialog)? I would rather
avoid to add another dialog before to ask if the user wants batch or single
file execution. Alternatively if there is a Fiji-specific solution I could
be happy with it.

Thanks for your help,

Christophe

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Dialog that can return both a folder or a file in ImageJ Javascript

Nicolas Chiaruttini
Here’s an option not far from what you’d like, using IJ2 scripts parameters:

#@File[] listOfPaths(label="select files or folders", style="both")

print("There are "+listOfPaths.length+" paths selected.");

for (i=0;i<listOfPaths.length;i++) {
        myFile=listOfPaths[i];
        if (File.exists(myFile)) {
                print(myFile + " exists.");
                if (File.isDirectory(myFile)) {
                        print("Is a directory");
                } else {
                        print("Is a file");
                }
        }
}



In brief, if you allow the user to make multiple selections, you can select both files and folders by using the tag style=both. The user can select only one file or one folder, but a list will be returned in any case.


If you force a single file to be selected with the #@File annotation without brackets, right now I believe you are forced to specify if you want a file or a folder.

Other workarounds:

  *   ask for a file selection and add a boolean parameter which means “process the folder where the file is”
  *   make your script with a single file input and make a second script with a folder input which calls the first script

Hope you’ll find something useful!

Nicolas


________________________________
De : ImageJ Interest Group <[hidden email]> de la part de Christophe Leterrier <[hidden email]>
Envoyé : vendredi, 6 avril 2018 21:12:02
À : [hidden email]
Objet : Dialog that can return both a folder or a file in ImageJ Javascript

Hi,

(I asked this on the ImageJ forum but I figured it's more an IJ1 question
and some people may have an answer here)

I have javascripts that I need to duplicate to do the same operation either
on a single file or on all the files within a folder (batch). The code
could be easily merged in a single script except for the choice of the
source by the user:
- to choose a file I use ImageJ OpenDialog class
- to choose a folder I use ImageJ DIrectoryChooser class

Sadly, using OpenDialog I can't select a file (and I can't select a file
using DirectoryChooser). Is there a way to have a file choosing dialog that
would let the user select a file or a folder (selecting it and click OK for
a file and folder, or clicking OK without selection to choose the current
folder, similar to the system file and ImageJ open dialog)? I would rather
avoid to add another dialog before to ask if the user wants batch or single
file execution. Alternatively if there is a Fiji-specific solution I could
be happy with it.

Thanks for your help,

Christophe

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html