Macro if/else problem [HELP]

imagej
macro
condition

#1

Hi guys , need ur help ~ ^^
I have two condition.

if
my Macro Action Tool (macro icon ) is pressed only one time for one image / process,
run the macro function nicely.
else
macro icon is pressed twice or > twice accidentally ,
print( “Warning”) and stop the macro function.

How to define this macro function as condition?
How to write the if /else statement?

macro "Measure IMC Action Tool - C00fT4e16%" {
run("8-bit");
setBackgroundColor(0, 0, 0);
run("Clear Outside");
run("Smooth");
setAutoThreshold("Default dark");
//run("Threshold...");
run("Measure");
}

#2

Good day!

if (my Macro Action Tool (macro icon ) is pressed only one time for one image / process) {
run the macro function nicely;
} else {
print( “Warning”) and stop the macro function;
}

I fear this isn’t possible with the ImageJ macro language.

Regards

Herbie


#3

that’s why I have no idea how to define the number of macro icon presses.
macro gt function of counter?


#4

Could an autorun macro set a flag that is tested and cleared by your macro?
And if you need to know for an image how many times the macro is executed, you might want to keep a (pseudo associative) array indexed by getImageID, containing 1 after execution of the macro.


#5

how to use getImageID ? I am new to image j …
do u think it is possible to do ?
if (condition)
statement;
else
statement;


#6

Did you find this list of functions and macros?
Then you will have seen the function getImageID(), which returns the unique negative number that is assigned to an opened image.
Keep track of these IDs in an expandable array indicating an image has already been processed.


#7

so how to use the negative number?
when I opened one image , the log window display -2 .
still blur @@


#8

When you start your processing job, initialise the array.
Then, in your processing macro, get the image ID.
Next, check if the imageID is in your array (loop over the array and compare).
If the ID is stored in the array, then skip the actual processing of this image.
Otherwise, process the image and add the image ID to the array.
The array will eventually be filled with negative numbers; the IDs of the images you have processed.


#9

which function is used to initialise the array ?


#10

You can use expandable arrays. Here is an example I got from Wayne:

 setOption("ExpandableArrays", true);
 cars = newArray;
 cars[0] = "Saab";
 cars[1] = "Volvo";
 cars[2] = "BMW";
 cars[4] = "Ford";
 Array.print(cars);

The setOption is not in the manual page (yet), most other things you can read for yourself in the pages mentioned above. Otherwise google for ‘expandable array ImageJ’.


#11

but I dun know how to write in macro programme…
something like this ? @@

setOption(“ExpandableArrays”, true);
image = newArray;
Array.print(image);

macro "Measure IMC Action Tool - C00fT4e16%" {
run("8-bit");
setBackgroundColor(0, 0, 0);
run("Clear Outside");
run("Smooth");
getImageID();
setAutoThreshold("Default dark");
//run("Threshold...");
run("Measure");
}

if (ID is stored in the array)

skip ;

else 
 process the image and add the image ID to the array.

#12

Yes, indeed. Add another macro that does the initialisation of the array and do put the if(){}else{} command inside your macro.
Study the function append in this example. I guess with the code below you will be able to figure it out.

Do use { and } to make a compound statement or only the first line after the if will be processed:

var processedFlags;//use var to get a global variable accessible from the functions and macros.
macro "Initialise"{
	initialise();
}
macro "Process"{
    testID=-5;//test data only, use imageID in production environment
	process(testID);
	process(testID);
}
function process(thisID){
	if(in_array(processedFlags,thisID)){
		print ("already processed image with ID "+thisID );
	}else{
		print ("processing image with ID "+thisID);
		processedFlags=append(processedFlags,thisID);
	}
}
function initialise(){
    setOption("ExpandableArrays", true);
    var processedFlags = newArray;
    return;
}
function in_array(theArray,theElement){
	for(i=0;i<theArray.length;i++){
		if(theArray[i]==theElement){
			return true;
		}
	}
	return false;
}
 function append(arr, value) {
     arr2 = newArray(arr.length+1);
     for (i=0; i<arr.length; i++)
        arr2[i] = arr[i];
     arr2[arr.length] = value;
     return arr2;
 }