Determine Macro Directory

fiji
imagej
macro

#1

Hi,

is there a stable way to get the directory / file path of the macro that is currently active?

I have a macro that is triggered via a shortcut (F10). Upon starting it reads out a txt file with scaling information that is in a subfolder of the directory of the macro. When I have the macro directory I can always define the location of the txt file relatively to it. e.g. “macro_path\subfolder”

File.directory();

seems to give me partially what I want. Problem is: it changes, and is not a stable permanent solution.

It returns the path of the last file/image/macro opened. Which is fine, when I install the macro and so the macro is the last thing opened. But the macro itself opens images, which redirects the meaning of File.directory() to that path (of the opened image).

The next time I trigger my macro using the shortcut it crashes, because the path name to the subfolder is now non-sensical (“image_path\subfolder”)

Is there a better way to get the macro directory path?

Thank you!


#2

@harritor

Maybe try with Script Parameters as opposed to the File.directory() calls… that way the macro directory will be ‘saved’ and won’t change throughout…

So add at the top of your macro script this line:

#@File(label = "Macro directory", style = "directory") macroPath

Then you can use the variable macroPath within your script instead. You can save other directories too if needed - just add another Parameter…

Just give it a try and see if it helps… if you have more questions, etc. - just ask! We are here to help.

eta :slight_smile:


#3

Hi eta!

Thank you for your response, I appreciate your effort!

I’ve tried the command both in my macro and also on it’s own in a new .ijm script.
Both times it gave me an error message telling me “Statement cannot begin with ‘#’ in line 1”

Am I doing something wrong? How can I solve this problem, it sounds very promising, but I feel like there is a small bug in here…

Thank you!


#4

@harritor

Hmmm… weird. Are you running an updated version of Fiji?? What I provided was the updated notation for Script Parameters… so you can always try the ‘older’ version:

 // @File(label = "Macro directory", style = "directory") macroPath

In this case - this line (and all other Script Parameters you might use) - need to be at the very top of your script - before anything else including comments. That rule doesn’t apply for the newer notation #@.

If that doesn’t do the trick - send us your code so we can try to reproduce the error ourselves as well.

eta


#5

Hmm, it still says “undefined variable” in an error message. in the Debug Window, there is also no variable of the name “macroPath”, seems that the command doesn’t save it as a variable, that can be recalled. So the first instance where I call macroPath gives me the “undefined variable” error.

The same happens, when I use this simple code for testing:

// @File(label = "Macro directory", style = "directory") macroPath

print(macroPath);

I saved this little piece of code as “test.ijm”, installed it in ImageJ via “plugins\macros\install…” and then executed it from the “plugins\macros” dropdown menu.

Same error.

Actually the preceding “#” gives me the “statement cannot begin with #” error.
Whereas the preceding “//” gives me the “undefined variable” error.

My ImageJ is 1.52d with Java 1.6.0_24 (64-bit)


#6

@harritor

Hmmm again… So - I’m not the most technically savvy ‘helper’ on here. SOrry for that! But what I would do is try with an updated version of Java. To do this - try just downloading the most-updated version of Fiji (comes with Java 8 - the update site for Java 8 should be enabled) - and open the Script Editor. Simply copy/paste that code example you just posted

and select the IJ1 Macro language and run the script. It works for me on my machine (with both script parameter notations).

Just try that - and see if that works at least at the moment… then we can take the next step if you need.

eta


#7

I’ve tried it all, downloaded the newest ImageJ bundled with Java 8. Tried the daily build of ImageJ as well. 1.52d seems to be the newest ImageJ. Java was 1.8.0

The script still behaves the same way with the errors :frowning: (when I install the macro and run ith via plugins\macros)

When I open the script in the Script editor in IJ1 Language and hit “Run” it opens a window of the file explorer to choose a directory…

Could you tell me why you are able to use the script commands? Is there a site with information and instruction on this? Do you have anything else installed that is required?

Thank you!


#8

@harritor

Ok. So …

so that worked. that’s good news…

what you are working with and writing - is a script. So try instead to install that file (making sure you use the correct file type extension… so .ijm for IJ1 Macros language) in the scripts folder - not the plugins\macros folder. Make sure to use underscores for spaces, etc.

That should work. It’s indicated in the Scripting Workshop on this slide - you can fast forward to 33:15 for the saving scripts part.

Let’s see if that does it!

eta :slight_smile:


#9

@harritor
To clarify things a bit more on this…

According to my colleague, @ctrueden, “Script parameters are a feature of ImageJ2. They will not work in plain ImageJ1. The Fiji distribution of ImageJ is built on ImageJ2, so as you point out, they also work in Fiji.”

So again - try using the Fiji distribution of ImageJ… I know this whole thing between Fiji/ImageJ and then ImageJ2/ImageJ1.x can get a bit confusing - so you can always read up a little on the flavors of ImageJ if that helps.

Sorry if things were not clear before. Just write again if you need help - there are also others here that can perhaps provide better answers, etc.

eta