Saving to the desktop as a default



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?



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

#@ File(label="Select a directory", style="directory") 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.



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


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




I tried it in FIJI

How is this different from?

myDir=getDirectory(“Choose a Directory”);


Script Parameters don’t work with plain ImageJ.


myDir=getDirectory(“Choose a Directory”);

is without Script Parameters!

The code Ellen proposed is with Script Parameters!




Is there a functional difference between the two approaches?


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.





call("", "~/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.



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 …



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.




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




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.



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?




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."



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:


I tried

call("", "~/Desktop");
call("", "~/Videos");   and
call("", "~/Documents");

but the default was the Desktop in all cases.

Here’s the entry in the API


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

Sets the current working directory.


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

This is confusing!


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.


@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.