Threshold image via Macro

Dear Imagej users,

thresholding my image within ImageJ in the HSB color space works,
and I get a thresholded image.

When I translate this to a macro, i.e.,

min=newArray(3); 
max=newArray(3); 
filter=newArray(3); 
a=getTitle(); 
run("HSB Stack"); 
run("Convert Stack to Images"); 
selectWindow("Hue"); 
rename("0"); 
selectWindow("Saturation"); 
rename("1"); 
selectWindow("Brightness"); 
rename("2"); 
min[0]=0; 
max[0]=207; 
filter[0]="pass"; 
min[1]=0; 
max[1]=255; 
filter[1]="pass"; 
min[2]=0; 
max[2]=255; 
filter[2]="pass"; 
for (i=0;i<3;i++){ 
  selectWindow(""+i); 
  setThreshold(min[i], max[i]); 
  run("Convert to Mask"); 
  if (filter[i]=="stop")  run("Invert"); 
} 
imageCalculator("AND create", "0","1"); 
imageCalculator("AND create", "Result of 0","2"); 
for (i=0;i<3;i++){ 
  selectWindow(""+i); 
  close(); 
} 
selectWindow("Result of 0"); 
close(); 
selectWindow("Result of Result of 0"); 
rename(a); 

then, this will not threshold my image?
I’ve the same code for RGB color space which works.
Wondering why this is not happening here for HSB color space.

Hope somebody can help out.

Best wishes,
Stephan

If one uses this kind of code,
better run("Close All"); at the very first line of code to make sure
you’re at a clean state… then this works.

Best wishes,
Stephan