Open new file automatically as it is created in a folder

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

Open new file automatically as it is created in a folder

rrmarino
Hello All,



Is there any macro/plugin that automatically opens a new file in a folder?
For instance, when scanning pictures, as the scanner software creates a
file in a folder I want Imagej to open it automatically.


Best regards everyone

Rodrigo Marino
Reply | Threaded
Open this post in threaded view
|

Re: Open new file automatically as it is created in a folder

dscho
Hi Rodrigo,

On Thu, 17 Nov 2011, Rodrigo Marino wrote:

> Is there any macro/plugin that automatically opens a new file in a folder?
> For instance, when scanning pictures, as the scanner software creates a
> file in a folder I want Imagej to open it automatically.

That'll require some macro programming. You probably need the
getFileList() function to get the list of the files in the directory, the
Arrays.sort() function and a function to search in a sorted list to
prevent already-opened files from opening again, and the wait() function
to wait between polling the directory.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: Open new file automatically as it is created in a folder

dscho
Hi,

On Wed, 16 Nov 2011, Johannes Schindelin wrote:

> On Thu, 17 Nov 2011, Rodrigo Marino wrote:
>
> > Is there any macro/plugin that automatically opens a new file in a
> > folder?  For instance, when scanning pictures, as the scanner software
> > creates a file in a folder I want Imagej to open it automatically.
>
> That'll require some macro programming. You probably need the
> getFileList() function to get the list of the files in the directory, the
> Arrays.sort() function and a function to search in a sorted list to
> prevent already-opened files from opening again, and the wait() function
> to wait between polling the directory.

Actually, due to some peculiarities it was not all that easy, but here is
what I wrote and which works here:

-- snipsnap --
directory = getDirectory("Select the directory");

// get the initial list (without opening the files)
list = getFileList(directory);
Array.sort(list);

for (;;) {
        previousList = list;
        list = getFileList(directory);
        if (previousList.length != list.length) {
                Array.sort(list);

                // walk both lists
                j = 0;
                for (i = 0; i < list.length; i++) {
                        stopJ = previousList.length;
                        while (j < stopJ)
                                if (previousList[j] < list[i])
                                        j++;
                                else
                                        stopJ = j;
                        if (j >= previousList.length)
                                open(directory + list[i]);
                        else if (list[i] != previousList[j])
                                open(directory + list[i]);
                }
        }

        wait(1); // wait a second
}
Reply | Threaded
Open this post in threaded view
|

Re: Open new file automatically as it is created in a folder

rrmarino
Hey Johannes that is beautiful! Thank you very much for writing the code. I
will try it and let you know.

Thanks,

Rodrigo

On Thu, Nov 17, 2011 at 1:56 PM, Johannes Schindelin <
[hidden email]> wrote:

> Hi,
>
> On Wed, 16 Nov 2011, Johannes Schindelin wrote:
>
> > On Thu, 17 Nov 2011, Rodrigo Marino wrote:
> >
> > > Is there any macro/plugin that automatically opens a new file in a
> > > folder?  For instance, when scanning pictures, as the scanner software
> > > creates a file in a folder I want Imagej to open it automatically.
> >
> > That'll require some macro programming. You probably need the
> > getFileList() function to get the list of the files in the directory, the
> > Arrays.sort() function and a function to search in a sorted list to
> > prevent already-opened files from opening again, and the wait() function
> > to wait between polling the directory.
>
> Actually, due to some peculiarities it was not all that easy, but here is
> what I wrote and which works here:
>
> -- snipsnap --
> directory = getDirectory("Select the directory");
>
> // get the initial list (without opening the files)
> list = getFileList(directory);
> Array.sort(list);
>
> for (;;) {
>        previousList = list;
>        list = getFileList(directory);
>        if (previousList.length != list.length) {
>                Array.sort(list);
>
>                // walk both lists
>                j = 0;
>                for (i = 0; i < list.length; i++) {
>                        stopJ = previousList.length;
>                        while (j < stopJ)
>                                if (previousList[j] < list[i])
>                                        j++;
>                                else
>                                        stopJ = j;
>                        if (j >= previousList.length)
>                                open(directory + list[i]);
>                        else if (list[i] != previousList[j])
>                                open(directory + list[i]);
>                }
>        }
>
>        wait(1); // wait a second
> }
>
Reply | Threaded
Open this post in threaded view
|

Re: Open new file automatically as it is created in a folder

Michael Doube-3
In reply to this post by rrmarino
Hi Rodrigo,

> Is there any macro/plugin that automatically opens a new file in a folder?
> For instance, when scanning pictures, as the scanner software creates a
> file in a folder I want Imagej to open it automatically.

Java also has a proper directory watch service:
http://download.oracle.com/javase/tutorial/essential/io/notification.html

But I don't know if anyone has implemented it in an ImageJ plugin.

Michael
Reply | Threaded
Open this post in threaded view
|

Re: Open new file automatically as it is created in a folder

Norbert Vischer
In reply to this post by rrmarino
Hi Rodrigo,

Johannes gave you a solution that works on any computer. (However, Johannes, you probably meant wait(1000) instead wait(1), otherwise you have 1000 disk accesses per second.)

Here below is an alternative option using Folder Actions in Automator that is built into OS X. It doesn't need a macro running, and also works if ImageJ is not open yet. It is not limited to pictures, but accepts any file that can be opened, like rois, text files, macros. And it works for aliases.

For this test, I was using OS X 10.6, and before following these instructions, I created a folder "To_ImageJ" on the desktop.

1. Open application "Automator"

2. Select icon "Folder Action" and click "Choose"

6. At the top, click "Choose Folder", select the folder "To_ImageJ",
  and click "Choose"

3. From the many actions listed at the left, pick the icon left
  to "Open Finder Items" and drag it to the panel on the right
  hand side. A PopUp menu appears called "Open With:"

4. Choose "Other.." at the bottom of the list, and navigate to
  your ImageJ.app or Fiji.app application, then click "Choose"  

7. Choose "File>SaveAs.." and save this action; I called it "OpenInImageJ"
  You can now quit Automator.

Now test it by dragging a file into folder "To_ImageJ".



Norbert Vischer
Research engineer
Centre for Advanced Microscopy
Swammerdam Institute for Life Sciences (SILS)
Science Park 904
1098 XH Amsterdam, the Netherlands