Automating the selection of windows in imagej

I’m trying to automate with macro for performing image manipulation/making composites but I’m not sure how to go about changing the script when it comes to selecting the correct corresponding windows and channels (they are all in the same file).

Automating the merging parts/thresholds is fine, but I can’t get the part on subselecting the channel:

// this is a very specific selection so I tried substringing (eg. to get C02 and Imagenumber separately because there are many more images with the same channel but only 2 windows are corresponding to each other)

The following doesn’t work too:

imageNum = substring(imageTitle, 0, 8);
imageTitle_channel = substring(imageTitle, 10, 13);
c1 = “C01.tif”
c2 = “C02.tif”
run(“Merge Channels…”, “c1=C01.tif c2=C02.tif create keep”);

Saving as jpeg/png file format too also is another issue, since I want to keep all resulting images in a different folder with the corresponding imagenumbers.

How do I get some R values too, if I wanted to find the correlation of the intensities that the 2 channels (in the composite image)?

Was there anything that I missed out?