Loop a subset of commands n times

I am trying to rewrite my macro to allow it to be run n times on the same image, where n is user defined. The macro opens a time series, waits for user to select a line, generates a kymograph and saves the kymograph with a filename corresponding to the original image. I would like to generate a loop/repeat command between lines 8 and 14, and keep all the filenames related to the original e.g. title + kymo_n, title + kymo_n+1 etc.

dir1 = getDirectory("Choose  Directory ");
dir2 = getDirectory("Choose  Directory ");
list = getFileList(dir1);
for (i=0;i<list.length;i++) {
      if (endsWith(list[i],".tif")){
      open(dir1+ list[i]);
title = getTitle();
waitForUser('Please go to 1st frame, select the process and press OK.');
run("MultipleKymograph ", "linewidth=5");
save(dir2 + title + "_kymo.tif");
run("Close All");

Thanks very much!

Hi @rozsn,

what are the problems you encounter in doing this?

Is the number of kymograms the same for each image or does it depend on the image?

Best regards,

Problems I’m encountering

  1. The command to execute the loop
  2. No. kymographs different for each image - I was thinking about introducing a dialogue box at the end of the loop for a command either to circle back to selecting a line or proceed with opening the next image.
  3. How to save with the right file name and not overwrite the previous kymograph.

Thanks so much!


  1. there are three different kinds of loops in the ij-macro-language:
  1. You could use getBoolean("message")within the loop to ask the user if he wants to create another kymogram for this image. See https://imagej.net/developer/macro/functions.html#G for details.

  2. Use a counter variable n, set it to 1 before the loop, append it to the filename using string concatenation and increment it by one at the end of the loop