Rewriting code in short form

Hello
I am new in macro coding and i wrote a code in macro for analyzing images for a customize filter.Code is running fine but this is too long and i need it in a short form. Can anybody suggest me how to rewrite it in a shorter format ?

Thanks.

// Bahlbi Teweldemedhin: Version 1.4.3.67 09.10.2019. 
// Advantage here: Direct comparability of the position number on image with the position number on the result table
// Extension of 6 options, individual determination of noise tolerance and further processing + total table of all images
// Applicable instead of TIFF only for jpg and tif files

run ("Clear Results");
print (" ")
close ("*")


dirX = getDirectory ("Select directory with images"); // Selection of the directory in which the pictures are located
list = getFileList (dirX);                          // List of files in the directory
path = getDirectory ("Storagepath");
Resultdirectory = path + "Segmented_Pics _ & _ Result_Tab";
File.makeDirectory(Resultdirectory);                 // creates folders to store the results
path2 = path + File.separator + "Segmented_Pics _ & _ Result_Tab" + File.separator;

var temp_list_length = 0; //Variable

for (i=0; i<list.length; i++)                         // loop
{
if (endsWith(list[i],"tif"))                 // Selection of all tif files
{
print(list[i]);                             // going through files (counting)
temp_list_length ++;
}
}


Step=newArray (1);                 // create a drop-down list to query what to do next
Step[0]="Measuring range is determined once by default for all images and there is a measurement table for each image extra";               // Option 1



Dialog.create ("Area to cut !");                         // generate query
Dialog.addChoice ("Selection", Step);
Dialog.show ();
step1 = Dialog.getChoice ();

// step 0 (1.option)

if (step1==Step[0]) {
waitForUser("Preferences", "Step 1 of 4: For the Preferences, please open an image");
waitForUser("Preferences", "Step 2 of 4: Please set the correct scaling in the following window.");
run ("Set Measurements...", "area mean standard modal min centroid center perimeter bounding shape feret's integrated median skewness kurtosis area_fraction stack display redirect = None decimal = 3");
run ("Set Scale...");                                                   // Scaling query

waitForUser ("Preferences", "Step 3 of 4: Select a rectangular area to measure");
getSelectionBounds (x, y, width, height); // Accept the coordinates to cut the image
Dialog.create ("New Image");
waitForUser ("Preferences", "Step 4 of 4: In the following, determine the noise tolerance and the light background setting should be included");
Dialog.addNumber ("Noise tolerance", 0); // enter tolerance (see: Find Maxima)
Dialog.show();
tol = Dialog.getNumber();          // tolerance number, which is used repeatedly in the loop

Dialog.addCheckbox("LightBackground", false); 
// Boolquestion
Dialog.addCheckbox("Include itemnumber", false);

lgt = Dialog.getCheckbox();
posnum = Dialog.getCheckbox();
Dialog.show();
Output_pic = newArray(3);                // create a drop-down list to query what to do next
Output_pic[0] = "Single Points";                 // Option 1

Output_pic[1] = "Maxima Within Tolerance";       // Option 2

Output_pic[2] = "Segmented Particles";           // Option 3

Dialog.create("Selection Output type!");

// generate query      
Dialog.addChoice("Selection", Output_pic);
Dialog.show();
art= Dialog.getChoice();
print ("********************************************************************************************* ******************* ");
print("Version 1.0");
print("Tolerance = " + tol+"   "+ art);
if(lgt==true) {
print("& Light Background");
File.makeDirectory(path2 + "Segmented Pics_Light Background" + "Noise tolerance_" + tol + "_" + art);
File.makeDirectory(path2 + "Results_Light Background" + "Noise tolerance_" + tol + "_" + art);
arg=getImageInfo ();
showText(arg);
selectWindow("Untitled");
save(path2 + File.separator + "Results_Light Background" + "Noise tolerance_" + tol + "_" + art + File.separator + "Setting_Scaling.txt");
run ("Close");
} else {
File.makeDirectory(path2 + "Segmented Pics" + "Noise tolerance_" + tol + "_" + art);
File.makeDirectory(path2 + "Results" + "Noise tolerance_" + tol + "_" + art);
arg=getImageInfo ();
showText(arg);
selectWindow("Untitled");
save(path2 + File.separator + "Results" + "Noise tolerance_" + tol + "_" + art + File.separator + "Setting_Scaling.txt");
run ("Close");
};
print("Data_Name" + "" + "\ t" + "Width" + "" + "\ t" + "Height"); // headers for log file (column name)
close();

for (i=0;i<list.length;i ++)                     // loop (go through images)
{
if (endsWith(list [i],"tif") || endsWith (list [i], "jpg"))    // If file ends with tif, then do the following !!!!!
{
open (dirX + list[i]);         // open files
makeRectangle(x,y,width,height);       // cut according to previously given coordinates
run("crop");

OPTION 1 ----------------------------
if(art == "Segmented Particles") {
if(lgt == true) {
run("Find Maxima..", "noise=tol output=[Segmented Particles] exclude light"); // Use of Lightbackground, if input: true
}else {
run ("Find Maxima...", "noise = tol output = [Segmented Particles] exclude");
}
name2=getTitle;

dir=getDirectory("image"); // The file gets the name of the picture
name = getTitle;

dotIndex=indexOf(name, ".");

name=substring(name, 0, dotIndex);
print(list[i] + "     " + "\ t" + getWidth () + "    " + "\ t" + getHeight () + "   " + art);

run("Add Image...", "list [i] x=0 y=0 opacity=70"); // superimpose the pictures
if(posnum==true) {
run ("Analyze Particles...", "show = Outlines display exclude summarize add");
run ("add image...", "list [i] x = 0 y = 0 opacity = 70"); // superimpose the pictures
}else {
run("Analyze Particles...", "display exclude clear summarize");
run("add image...", "list [i] x=0 y=0 opacity = 70"); // superimpose the pictures
select window (name2);
}
} //END-------------------------------

@tanzia52

A first step would be to transition things over to using Script Parameters.… that might trim your code down a lot. They are demo’d in the Scripting with Fiji workshop (here are the most updated slides). There is an example code you can run to play around with them - to see how they are rendered, etc. (Open the Script Editor and then Templates > Intro > Widgets (Beanshell or Javascript)).

thanks for your help…

1 Like