Login  Register

Re: How to save macro variables' values in a text file ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: How to save macro variables' values in a text file ?

Thomas Leonardo
2 posts
Dear ImageJ users,

I apologize if this question is already documented, I could not find
anything on the internet.

I am writing a macro which starts with a dialog box, allowing to set string
variables. I would like to record the values entered by the user into a
file (typically a text file), so that the next time the macro is used the
default values will be the one entered before (rather than the "factory
settings").

I can easily read variables values from a text file by using a function
like this one...

function getValues() {
  dataString=File.openAsString(initializationFilePath);
  rows=split(dataString, "\n");
  variable_1=rows[0];
  variable_2=rows[1];
}

...but I am unable to "update" the values in the text file, or even to
overwrite it.

Could you please give me some clues on how to do that?

Sincerely,
Thomas

PS: sorry for double-posting, the first message was inadvertently sent!

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: How to save macro variables' values in a text file ?

ctrueden
1670 posts
Hi Thomas,

> I would like to record the values entered by the user into a file
> (typically a text file), so that the next time the macro is used the
> default values will be the one entered before (rather than the
> "factory settings").

You can accomplish your goal using the ij.Prefs class, specifically the
Prefs.get and Prefs.set methods. These methods allow you to store settings
into the ImageJ preferences cache on disk for later retrieval, which sounds
like what you need.

From a macro, the following should work (though I did not test):

call("ij.Prefs.set", "myVariableKey", "desiredValueToSave");
previousValue = call("ij.Prefs.get", "myVariableKey", "defaultValue");

Be sure to choose a unique variable key to avoid potential name clashes
with other plugins & macros.

Regards,
Curtis


On Mon, Feb 18, 2013 at 3:30 PM, TL <[hidden email]> wrote:

> Dear ImageJ users,
>
> I apologize if this question is already documented, I could not find
> anything on the internet.
>
> I am writing a macro which starts with a dialog box, allowing to set string
> variables. I would like to record the values entered by the user into a
> file (typically a text file), so that the next time the macro is used the
> default values will be the one entered before (rather than the "factory
> settings").
>
> I can easily read variables values from a text file by using a function
> like this one...
>
> function getValues() {
>   dataString=File.openAsString(initializationFilePath);
>   rows=split(dataString, "\n");
>   variable_1=rows[0];
>   variable_2=rows[1];
> }
>
> ...but I am unable to "update" the values in the text file, or even to
> overwrite it.
>
> Could you please give me some clues on how to do that?
>
> Sincerely,
> Thomas
>
> PS: sorry for double-posting, the first message was inadvertently sent!
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: How to save macro variables' values in a text file ?

Thomas Leonardo
2 posts
Hi Curtis,

Thanks a lot, this works perfectly well and is much more convenient than
reading/writing a specific file! :)

Regards,
Thomas

2013/2/18 Curtis Rueden <[hidden email]>

> Hi Thomas,
>
> > I would like to record the values entered by the user into a file
> > (typically a text file), so that the next time the macro is used the
> > default values will be the one entered before (rather than the
> > "factory settings").
>
> You can accomplish your goal using the ij.Prefs class, specifically the
> Prefs.get and Prefs.set methods. These methods allow you to store settings
> into the ImageJ preferences cache on disk for later retrieval, which sounds
> like what you need.
>
> From a macro, the following should work (though I did not test):
>
> call("ij.Prefs.set", "myVariableKey", "desiredValueToSave");
> previousValue = call("ij.Prefs.get", "myVariableKey", "defaultValue");
>
> Be sure to choose a unique variable key to avoid potential name clashes
> with other plugins & macros.
>
> Regards,
> Curtis
>
>
> On Mon, Feb 18, 2013 at 3:30 PM, TL <[hidden email]> wrote:
>
> > Dear ImageJ users,
> >
> > I apologize if this question is already documented, I could not find
> > anything on the internet.
> >
> > I am writing a macro which starts with a dialog box, allowing to set
> string
> > variables. I would like to record the values entered by the user into a
> > file (typically a text file), so that the next time the macro is used the
> > default values will be the one entered before (rather than the "factory
> > settings").
> >
> > I can easily read variables values from a text file by using a function
> > like this one...
> >
> > function getValues() {
> >   dataString=File.openAsString(initializationFilePath);
> >   rows=split(dataString, "\n");
> >   variable_1=rows[0];
> >   variable_2=rows[1];
> > }
> >
> > ...but I am unable to "update" the values in the text file, or even to
> > overwrite it.
> >
> > Could you please give me some clues on how to do that?
> >
> > Sincerely,
> > Thomas
> >
> > PS: sorry for double-posting, the first message was inadvertently sent!
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html