Login  Register

Re: isOpen("path") ?

Posted by dscho on Mar 16, 2013; 6:48pm
URL: http://imagej.273.s1.nabble.com/isOpen-path-tp5002210p5002224.html

Hi Norbert,

On Sat, 16 Mar 2013, Norbert Vischer wrote:

> Is there a simple way to check if a stored image is open?

Unfortunately, no simple way (the macro language is too simple for true
object-oriented programming and that's what you essentially require).

> For example,
>
>    isOpen("/Users/norbert/Desktop/blobs.gif")
>
> would be nice, but seems not to be supported, nor did I find anything in
> the File.* commands.
>
> ( I know that I could travel through all open windows and compare paths..)

That would work, of course, but it would be slow:

-- snip --
// This function detects path -> image-index pairs
// You can use List.get(path) to get the image-index
// and selectImage(index + 1) to make it the active image
function getOpenedPaths() {
        List.clear();
        count = nImages();
        for (i = 0; i < count; i++) {
                selectImage(1 + i);
                path = getInfo("image.directory") + getInfo("image.filename");
                List.set(path, i);
        }
}
-- snap --

You would use it that way:

-- snip --
getOpenedPaths();

path = "/path/to/my/file.tiff";

id = List.get(path);
if (id == "") {
        print("Not open: " + path);
} else {
        selectImage(1 + id);
}
-- snap --

This would be slow and you would want to make sure that you run this only
in batch mode.

Unfortunately, there are some obstacles if you want to accelerate it, e.g.
by going Javascript:

-- snip --
// This function detects path -> image-index pairs
// You can use List.get(path) to get the image-index
// and selectImage(index + 1) to make it the active image
function getOpenedPaths() {
        // we need to use a system property since returning values from
        // eval("script", script) is broken in ImageJ 1.47m
        unused = call("java.lang.System.setProperty", "hopefully.unused", "");

        script = 'importClass(Packages.ij.IJ);'
                + 'importClass(Packages.ij.WindowManager);'
                + ''
                + 'var result = [];'
                + 'var count = WindowManager.getImageCount();'
                + 'for (var i = 0; i < count; i++) {'
                + ' image = WindowManager.getImage(1 + i);'
                + ' info = image.getOriginalFileInfo();'
                + ' if (info != null) {'
                + ' result.push(info.directory + info.fileName + "=" + i);
                + ' }'
                + '}'
                + ''
                + 'System.setProperty("hopefully.unused", result.join("\\n"));';
        x = eval('script', script);
        List.setList(call("java.lang.System.getProperty", "hopefully.unused"));
}
-- snap --

Ciao,
Johannes

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