I’m trying to allow a script in ImageJ/Fiji to open another text file which is located in the same folder as the script. Therefore, the script would like to know the folder in which it is located in…
I had a look in the script service interface, but this one apparently doesn’t offer a getter for this information. Furthermore, the IJ.getDirectory(“current”) is also just in special cases pointing to the right directory…
This will give you the full path name of the directory that contains
the .py file that contains this code snippet. So if you run a script
that calls a module that calls a module that contains this code,
you won’t get the directory of the top-level script, but rather, the
directory of the second module.
I have used this for more or less your use case – opening data
files in the directory of the script (or in a separate data directory
that is in a fixed location relative to the directory of the script).
(I don’t know how or whether this can be done with other scripting
languages, or whether there is any sort of language-neutral way
to do it.)
I’m afraid but this won’t work as well. Just imagine, the user opens the script, then opens some images and does other stuff. After some time, the File.directory() function will no longer contain the folder of the script.
(Note, if you type this code into the script editor – so there is no
file – rather than opening a file,
will resolve to None, so the call to abspath will error out.)
I could have sworn that I had run my original suggestion in
Fiji (but maybe I did, and maybe I didn’t). I did, somehow,
in the past, get the location of a script in Fiji, but maybe it
wasn’t with python.
Anyway, I wrote my original post from memory, rather than
verifying as I posted. (I know, bad idea.) I don’t know what
happened here – maybe I had tweaked my environment
somehow, or maybe I was mixing up scripting languages.