MacroRecorder ImageJ language

@Wayne @haesleinhuepf

I am writing own code for macro recording in ImageJ and I do not understand the difference between those options:

Recorder.recordString( String s );
Recorder.recordCall( String s );

Do you know which one I just use to record “Java-style” (e.g. jython) executable commands?

1 Like

Hi @Christian_Tischer,

in this case, the source code is your friend: The first method you mentioned will put the text in the recorder as it is. The second one does some magic.

Let us know if this answers your question as I’m not sure if I understood it :-/

Cheers,
Robert

1 Like

Yeah, I looked at it but could not quickly decipher whether the magic is something I want or not. So maybe I’ll just go with the the recordString then.

1 Like

Related question: how do I programatically set the recording language?

I did not find a method called setScriptMode( ) or similar…

1 Like

Hi @Christian_Tischer,

here is an example in jython on how to set the recording language

from ij.plugin.frame import Recorder
from ij import Prefs

pf = Prefs()
# options are Macro, JavaScript, BeanShell and Java
mode = "BeanShell"
pf.set("recorder.mode",mode)
rc = Recorder()

Notice that in this example the Recorder window needs to be closed before launching the script.
Hopefully it helps you towards the right direction.

1 Like

Hi @gcardone,
Thanks! I can reproduce this with the python script.
Weirdly, within Java and IntelliJ it does not work, but the language stays Macro

import ij.Prefs;
import ij.plugin.frame.Recorder;
import net.imagej.ImageJ;

public class Test
{
	public static void main ( String... args )
	{
		final ImageJ ij = new ImageJ();
		ij.ui().showUI();

		Prefs prefs = new Prefs();
		prefs.set( "recorder.mode", "JavaScript" );
		new Recorder();
     }
}

Maybe I am doing something wrong still?
Any ideas? (ping @Wayne)

@Christian_Tischer,

do you really need to create an instance of Prefs? I think it should be just Prefs.set(), but at the moment I can’t test it.

1 Like

in fact, no, without the instance it works :slight_smile:
I only created an instance because you also did it in your jython script… :slight_smile:

1 Like

For the record: In Java the solution is:

import ij.Prefs;
Prefs.set( "recorder.mode", "JavaScript" );

@imagejan @haesleinhuepf @Wayne
I my experience, after the ImageJ Macro language, Jython is the second most popular scripting language in ImageJ. Would it be possible to add this as a choice for the recording languages?

I found out that this “solution” does not seem to work changing the language of a running recorder. For this I need to use reflection (see below).
@Wayne, what do you think about providing an official API for this?

public class RecordingLanguageManager
{
	private final static String[] modes = {"Macro", "JavaScript", "BeanShell", "Java"};

	public RecordingLanguageManager()
	{
	}

	public void setLanguage( String language )
	{
		try
		{
			Prefs.set( "recorder.mode", language );

			Class  aClass = Recorder.class;
			Field field = aClass.getDeclaredField("mode");
			field.setAccessible( true );
			final Recorder recorder = Recorder.getInstance();
			Choice choice = (Choice) field.get(recorder);
			int languageIndex = Arrays.asList( modes ).indexOf(language);
			choice.select( languageIndex );

			Method setFileName = Recorder.class.getDeclaredMethod("setFileName" );
			setFileName.setAccessible( true );
			setFileName.invoke( recorder );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
	}
}

The Recorder in the ImageJ 1.53g26 daily build supports Python.

6 Likes