ImageJ matches() function - Solved

Hi there,

I am looking for writing a macro that checks the content of the RunAtStartup.ijm. I want to know if this file contain the following string :
run(“MyMacro”);

I got it to work with the Startswith(string, regex) or Endswith(string, regex) but it does not work with Matches(string, regex).
Is it because I am using getInfo(“window.contents”) to get the content of the RunAtStartup file?
Any help would be much appreciated

// Get the path of the Startup Macro
MacroFolder=eval("script", "IJ.getDirectory(\"macros\");");
StartUpMarcro="RunAtStartup.ijm";

// Read the file in as a text
run("Text File... ", "open="+MacroFolder+StartUpMarcro);

//Get the content of the text window
selectWindow(StartUpMarcro);
StartUpMacroContent=getInfo("window.contents");

// Close the text window window
selectWindow(StartUpMarcro);
run("Close");

// Get the Status of Mymacro
if(matches(StartUpMacroContent, ".*run(\"MyMacro\")\;.*")==1){
MyMacroStatus="active";} else {MyMacroStatus="inactive"; }

Did you try using

File.openAsString(path)

to directly load the file content as a string?

Thanks, it is a great suggestion. Unfortunately I just tried and no… it does not work…

Alright I think I got it.

The getcontent shall be split by \n into an array even if there is only one row.

Alright two things:

  1. the string need to be split
  2. the Regular expression needs \ for a " but needs \\ for a ( or a ;
// Get the path of the Startup Macro
MacroFolder=eval("script", "IJ.getDirectory(\"macros\");");
StartUpMarcro="RunAtStartup.ijm";

// Read the file in as a text
StartUpMacroContent=File.openAsString(MacroFolder+StartUpMarcro);

// Split the content for each line
StartUpMacroContentArray=split(StartUpMacroContent, "\n");

// Set the Status as 
MyMacroAutostartStatus="inactive";

// loop through the array to see if there is a Match and if so change the status to active
for (n=0; n<StartUpMacroContentArray.length; n++){
if(matches(StartUpMacroContentArray[n], "run\\(\"MyMacro\"\\)\\;")==1){
MyMacroAutostartStatus="active";
}
}

Then you can prompt the user if he or she wants to activate mymacro autostartup or desactivate it.

1 Like