Macro commands to paste info onto clipboard

Tags: #<Tag:0x00007fb882f0d088> #<Tag:0x00007fb882f0ce80> #<Tag:0x00007fb882f0cbd8> #<Tag:0x00007fb882f0c868> #<Tag:0x00007fb882f0c520>


Dear all,

I have written a macro to perform mathematical operations on an image stack using the Fiji implementation.
The final step of this macro is to plot the average intensity of each frame (Plot Z-axis profile).
I am interested in the values themselves, rather than on the plot ImageJ provides.
So, on the graph I have to click the LIST button so that I access a table and the values I then copy to the clipboard to paste in another application.

My question is: is there a command I can use in my macro script to get the values from the table and copy them straight onto the clipboard, so that after running the macro on a stack all I would need to do is to paste the values I get?

If copy is not possible, how can I call up the table with Z-axis average intensity profile from a macro command instead of having to click on the LIST button.

Many thanks for the input!!




Dear @rhjpires,

There are two bits of code that you need, I will just link to examples of them both below.

  1. Get the profile to the results table
  2. Copy the results table to clipboard

Essentially each value along the profile is added to the results table and then the values from the results table are made in to a string.

EDIT: I seem to be confused between “Plot Profile” and “Plot Z-axis Profile”, so I have edited my response below

What you can do is select all (Edit > Selection > Select All) and add to the ROI manager (Edit > Selection > To ROI Manager) then multi-measure that ROI in the ROI manager.

I think the IJ1 Macro code would be like this (not tested):

    run("Set Measurements...", "mean redirect=None decimal=3");
    run("Clear Results");
    run("Select All");
    roiManager("Multi Measure");
    for (i=0; i<nResults; i++)
        String.append(getResult("Mean", i) + "\n");




Thank you Rob, this was quite helpful but I could not set it to work.
It helped 'though to dig a bit deeper so I assembled something that does what I want

    //define what to measure from each slice
    run("Set Measurements...", "mean redirect=None decimal=3");
    //cleat Results tables from  possible previous run
    run("Clear Results");
    //go through each slice and store measurements on the Results table
    for (n=1; n<=nSlices; n++) {

    //this finishes by copying results from the table onto the clipboard

With this done, I was now thinking that it would actually be better that, instead of copying the table onto the clipboard, I might as well do a batch processing of my files where for each file I generate a CSV file containing the Results table with the same name as the original image stack file. Could you give me some pointers?

Many thanks,



I would recommend keeping this page bookmarked to use, it has the built-in macro functions of ImageJ and explains what they do, sometimes with examples.

To get the name of the image you can use getTitle(); to get the header name (this usually has the file extension at the end, if the image was only just opened). To get the file name without the extension you can replace the string of the file extension (e.g. “.tif”) with nothing. Example: replace(imgTitle, ".tif", "");. Or if the image is the last file opened, you can use File.nameWithoutExtension();.

Once you have this variable set (let’s call it filename), you can save the results table using saveAs("Results", dir + filename + ".csv"); where dir is the directory you want to save the .csv files in. You can use dir = getDirectory("Choose directory for saving"); to assign the variable, this opens a pop-up window so you can choose a directory.

Batch processing information can be found on this webpage, so you can apply your script to all of your files. Alternatively you can use the batch processing plugin (Process > Batch > Macro…).




Rob, this is really great. Many thanks for your help!

I got it to work so it now stores all results in a CSV file, this is great.
I also understand that the batch functionality using macros I can go through the files in a datafolder and chew everything up, and that works fine too, i get different CSV files.

Only one small detail, is that it seems that the batch functionality does not look into subfolders but only with files within a folder - is that correct? Can you advise me on how to implement looking into subfolders?

many thanks,



Excerpt from the linked forum post:


Cool! Thank you Rob, I will have a look into it!