Write in a txt file in a while loop

Hi everyone,

does anyone know why i am unable to write in a txt file while running a “while loop”?
Using the fragment of code below, I want to create a txt file to note different values and parameters along the macro running.
Here is the point

if (action=="choisir le dossier de destination"){
    output=getDirectory("Choisir le dossier dans lequel seront enregistrés les fichiers csv");
    print(Fichier, "Raw Parameters");
    print(Fichier, "output= "+output);

This create the file FIchier, but fail to write anything into.
But if I suppress the while loop, it does the work well.
I juste can’t understand why it is not possible to write into a txt file while in a loop.
Hope you can help me understand.

Thank you.

var action="";
var recom="Oui";
var Smin="2000";
var Smax;
var output;
var nSl;
var Fichier;

getDimensions(width, height, channels, slices, frames);
run("Set Measurements...", "mean standard display redirect=None decimal=3");

while (action!="quitter"){
Dialog.addMessage("Regler les Z mini et maxi au moment du pretraitement des images");
Dialog.addChoice("Alpha Actinine :", newArray("C1","C2","C3","C4"));//Dialog.addToSameRow();Dialog.addCheckbox("", true);
Dialog.addChoice("DAB2 :", newArray("C2","C1","C3","C4"));//Dialog.addToSameRow();Dialog.addCheckbox("", true);
Dialog.addChoice("Cell Mask :", newArray("C3","C1","C2","C4"));//Dialog.addToSameRow();Dialog.addCheckbox("", true);
Dialog.addChoice("DAPI :", newArray("C4","C1","C2","C3"));//Dialog.addToSameRow();Dialog.addCheckbox("", true);
Dialog.addNumber("Z-mini", 0); Dialog.addToSameRow();Dialog.addNumber("Z-max", nSl);
Dialog.addChoice("Action? :", newArray("choisir le dossier de destination","prétraitement des images", "Choix des ROIs","Seuils aggregats","analyse","analyse sans noyaux", "quitter"));


action = Dialog.getChoice();

if (action=="choisir le dossier de destination"){
    output=getDirectory("Choisir le dossier dans lequel seront enregistrés les fichiers csv");
    print(Fichier, "Raw Parameters");
    print(Fichier, "output= "+output);
else if(action=="prétraitement des images"){
else if(action=="Choix des ROIs"){
else if (action=="Seuils aggregats"){
else if (action=="analyse"){
else if (action=="analyse sans noyaux"){
else { break;}

Hi @JPolentes ,

I’m not sure if the print statment can write to files. Consider using File.append instead:

Let us know if this helps!


Hi @haesleinhuepf ,

thank you for your help. Unfortunately, replacing the print command by the File.append in my code command does not write anything on my file neither.
The print command is actually appropriated to write in a file created by the File.open command, as described in the Built-in Macro Functions page :

File.open(path) - Creates a new text file and returns a file variable that refers to it. To write to the file, pass the file variable to the print function. Displays a file save dialog box if path is an empty string. The file is closed when the macro exits. Currently, only one file can be open at a time. For an example, refer to the SaveTextFileDemo macro.

And actually it work very well as soon as i do not inclued it into a while loop. But when it is in the while loop, it create the file but do not write into anymore.

1 Like

Does this demo-macro work for you?

If yes, it shouldn’t be too difficult to adapt it to your needs.

1 Like


yes the demo macro works, and that is why i did not understand why it did not work in my own code.

But doing some tests, i finally understood something interesting (maybe it will be usefull for someone else): Actually the print command is doing the job and put all the values associated in a kind of temporary place, and write it in the txt file only when the macro is finished, or at least when the file is close by the File.close command.

1 Like

I think the problem in your code is that you do not close your txt file. Printing to the txt-file with print(Fichier, “some text”) in ImageJ macro works fine. Just put


at the end of your code. Hope this works!

Hi @EL_Pollo_Diablo ,

you are right, that is what I just deducted from my own tests. Actually, the File.close(Fichier) is only necessary if I need to write on the txt file before the end of the macro. If i do not use that command, the text write automatically at the end of the macro.

Thank you for your help anyway.