getArgument() equivalent for installed macros?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

getArgument() equivalent for installed macros?

Bill Christens-Barry-2
In ImageJ I would like to pass a value from a macro in a text window to a macro that has been installed.

The runMacro(name, arg) command will pass an argument to a macro that will be run from a file, and this argument can be handled in the called macro using the getArgument() command. But this entails opening the file containing a called macro, rather than running an already installed macro. Since I want the calling macro itself to NOT be installed (it will be run from a text window using the "Macros>Run Macro" menu item, it's not possible to use a global variable to pass this value.

I haven't found a comparable operation to pass values to an installed macro. Is there a suggested way of doing this?

Thanks.

Bill Christens-Barry

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

Re: getArgument() equivalent for installed macros?

Gabriel Landini
On Wednesday, 18 January 2017 10:19:45 GMT Bill Christens-Barry wrote:
> In ImageJ I would like to pass a value from a macro in a text window to a
> macro that has been installed.
 
> The runMacro(name, arg) command will pass an argument to a macro that will
> be run from a file, and this argument can be handled in the called macro
> using the getArgument() command. But this entails opening the file
> containing a called macro, rather than running an already installed macro.

Hi Bill,
Does it require to have the other macro open?
I had the idea (maybe wrong) that if the macro is just in the plugins folder
and is visible to IJ (i.e. it has a "_" in the name) you can call it from
another macro and the 2nd macro gets the argument the way you suggested.

Cheers
Gabriel

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

Re: getArgument() equivalent for installed macros?

Bill Christens-Barry-2
In reply to this post by Bill Christens-Barry-2
Gabriel,

Thanks for your suggestion that I try putting the called macro into the plugins folder so it would be automatically loaded. Unfortunately, this didn't give the hoped for result when I tried a test macro "Test_Macro" that simply prints the passed argument after using getArgument():

    macro "Test_macro" {
        theArg = getArgument();
        print("Test_macro actually ran and printed", theArg);
    }

After installing "Test_macro" into the plugins menu, I ran the following macro code from its text window:

    run("Test macro", "theArgString");
    runMacro("Test_macro", "theArgString");

        ==>

    Test_macro actually ran and printed
    Test_macro actually ran and printed theArgString

Note that the successful second command runs the macro from a file, while the unsuccessful first command line uses the installed version. It appears that run() can't suitably pass the optional second argument, while runMacro() can. It's important to me to have the called macro preloaded and that the caller not be loaded, which is why I'm not using a global variable.

Bill Christens-Barry

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

Re: getArgument() equivalent for installed macros?

Michael Schmid
In reply to this post by Gabriel Landini
> On Wednesday, 18 January 2017 10:19:45 GMT Bill Christens-Barry wrote:

>> In ImageJ I would like to pass a value from a macro in a text window to a
>> macro that has been installed.
>
>> The runMacro(name, arg) command will pass an argument to a macro that will
>> be run from a file, and this argument can be handled in the called macro
>> using the getArgument() command. But this entails opening the file
>> containing a called macro, rather than running an already installed macro.

On 18/01/2017 16:43, Gabriel Landini wrote:

> Does it require to have the other macro open?
> I had the idea (maybe wrong) that if the macro is just in the plugins folder
> and is visible to IJ (i.e. it has a "_" in the name) you can call it from
> another macro and the 2nd macro gets the argument the way you suggested.

Hi Bill,

if you find nothing else, there is one more option: passing variables
via the Preferences:

Write:
  call("ij.Prefs.set", "myMacros.myText", myText);

Read (with default "nothing yet", if the Prefs item has not been set):
  var myText = call("ij.Prefs.get", "myMacros.myText", "nothing yet");

The key, here "myMacros.myText", must be the same for read and write and
contain a period.

The key/value pair will be written to the user's IJ_Prefs.txt file. So
ImageJ will remember the value even if you close ImageJ and open it again.


Michael

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

Re: getArgument() equivalent for installed macros?

Bill Christens-Barry-2
In reply to this post by Bill Christens-Barry-2
Michael,

Your suggestion of using call("ij.Prefs.set") and call("ij.Prefs.get") works very well. After playing with it, I learned that the set and get methods respectively write to and read from memory and not to or from the IJ_Prefs.txt file. Apparently the IJ_Prefs.txt file gets written only when ImageJ is closed or read only when it is opened. This meets my hope to pass the values without the file system becoming involved.

Bill Christens-Barry

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