Saving to the desktop as a default

imagej
macro

#1

I’ve noted that if you manually save an image or text file to the desktop, subsequent saves default to the desktop. Is there a way to accomplish this focus on the desktop programmatically in a macro?


#2

@rondespain

I’d recommend using Script Parameters to select the directory you wish …

#@ File(label="Select a directory", style="directory") myDir
 
print(myDir)

Per default, variable values are persisted between runs of a script. This means that parameter values from a previous run are used as starting value. So if Desktop is selected on the first run… it will show up as the default for subsequent runs of that macro.

Of course - this is a more flexible option for those using your macro… that adaptability might come in handy down-the-road in case Desktop is no longer used as the main directory - then it’s not hard-coded.

eta


#3

Is this documented somewhere. My macro chocked when I added it.


#4

You can’t use this approach with plain ImageJ. You need Fiji …

Regards

Herbie


#5

I tried it in FIJI

How is this different from?

myDir=getDirectory(“Choose a Directory”);
print(myDir);


#6

Script Parameters don’t work with plain ImageJ.

This

myDir=getDirectory(“Choose a Directory”);
print(myDir);

is without Script Parameters!

The code Ellen proposed is with Script Parameters!

HTH

Herbie


#7

Is there a functional difference between the two approaches?


#8

I prefer the way compatible with plain ImageJ, even if it should not provide the same functionality, and I’m not familiar with Script Parameters because I don’t use Fiji. BTW, the syntax appears being ugly.

Regards

Herbie


#9

@rondespain

call("ij.io.OpenDialog.setDefaultDirectory", "~/Desktop");

was published in the past as funcitonality in ImageJ proper. With me, it still works; if I subsequently use the Open dialog (from the File menu), it gets me started in Desktop; if I change the argument above into “~/Documents”, the dialog takes me to Documents.
Strangely enough, a getDirectory() after this seems to ignore the default directory. A saveAs(“file.ext”) opens a dialog with the default directory as a starting point.


#10

@rondespain

Check out the link I gave you above… there you’ll read " All scripting languages have access to a universal #@parameter notation for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x GenericDialog because it is totally agnostic of the user interface, allowing such scripts to run in a variety of contexts." … Script Parameters give you more ‘power’ down-the-road … you won’t be tied to ImageJ1.x in this case.

But really - you can use either …

eta


#11

Good day Ellen,

this formulation is funny

you won’t be tied to ImageJ1.x in this case.

because Script Parameters simply don’t work with plain ImageJ, aka ImageJ-1.

Regards

Herbie


#12

atarena
Are these #@ parameter calls documented somewhere? They look like they are worth understanding.

Ron


#13

@rondespain

Just follow the link I provided above… here it is again:

There are more links hidden in there too that provide more info… is there something specific you are asking for? Maybe something is missing in that documentation that we can improve upon.

eta


#14

Thanks eta. The link just looked like a title, so I missed it. I wonder if these calls would work in Matlab, or Bash scripts? A universal scripting syntax would be great. Do you know the meaning of the # and @ at the front of the calls?

Ron


#15

@rondespain

I’m not sure they’ll work with either Matlab or Bash - though I might be wrong… others here may know better regarding passing parameters in these cases.

There is no ‘meaning’ per se… It’s just the shared syntax. If you click that link above that I sent you, in the Basic Syntax section it states: " Parameter declarations begin with #@ . Each such line contains a single parameter declaration or script directive and nothing else."

eta


#16

The ImageJ-MATLAB project provides SciJava scripting for MATLAB, including support for script parameters.

Bash and other shell scripts are not SciJava scripts. However, they can invoke ImageJ, and pass along a SciJava script of your choice for execution. See this page for details:

That’s right—the parameter syntax is a feature of SciJava scripting. So these scripts will work the same in ImageJ2, KNIME, OMERO, CellProfiler, and any other project supporting them. For details, see the ImageJ2 paper:


#17

I tried

call("ij.io.OpenDialog.setDefaultDirectory", "~/Desktop");
call("ij.io.OpenDialog.setDefaultDirectory", "~/Videos");   and
call("ij.io.OpenDialog.setDefaultDirectory", "~/Documents");

but the default was the Desktop in all cases.

Here’s the entry in the API

setDefaultDirectory

public static void setDefaultDirectory(java.lang.String defaultDir)

Sets the current working directory.

e.g.

codeDir=getDirectory("Choose a Code Directory") //Choose the directory with the .exes in it
call("ij.io.OpenDialog.setDefaultDirectory", "~/Desktop");
exec(codeDir+"FSCapture.exe");  // a screen capture utility
call("ij.io.OpenDialog.setDefaultDirectory", "~/Videos");
exec(codeDir+"FSCapture.exe");
call("ij.io.OpenDialog.setDefaultDirectory", "~/Documents");
exec(codeDir+"FSCapture.exe");
//each save from FSCaprure went to the desktop

This is confusing!


#18

Hi @rondespain,
By the looks of it (“FSCapture.exe”), you are on a PC while I am on a mac.
And they keep telling me “a PC can do what a mac can do, so why don’t you switch?”.
Thanks for another argument to counter that question :wink:

On the serious side: I am within an ImageJ1 macro, not in ImageJ2, nor in Java.
This might add more smoke and mirrors. Maybe I’m just lucky.


#19

@rondespain Are you sure using tilde (~) will work? It is a bash-ism, and not generally supported cross-platform, or outside of a shell scripting context.