Dealing with spaces in file names + defining directory


I just discovered the power of macros, and quickly coded something to help automate my imagej workflow. Here is what I wrote:

macro "merge channels [m]"{
    imageDir = getDir("imagej");
    RFPPath = File.openDialog(imageDir);
    GFPPath = File.openDialog(imageDir);

    RFPName = getTitle();
    run("RGB Color");
    GFPName = getTitle();
    run("RGB Color");

    run("Merge Channels...", "c1=" + RFPName + " c2=" + GFPName + " create");
    run("Input/Output...", "jpeg="+100);


I noticed that if the files selected have spaces, it will stop the Merge Channels from running because getTitle() does not include the file name after it sees a space. I read that implementing square brackets ([ and ]) are the solution to this issue, but I am unsure where it belongs.

The second issue I’m having is that I am unable to define a specific directory, most likely because I lack knowledge of the correct syntax required. I’ve settled on the imagej folder for now.

For example, I want to define a specific directory like C:\User\Folder in the code itself. If I change the code to allow me to select a specific directory, I would need to do so every time the macro is run, which is not desirable.

Thanks in advance for your time and assistance!

1 Like

Hi @fragmentsoflife ,

rectangular brackets should solve the issue with spaces in titles:

run("Merge Channels...", "c1=[" + RFPName + "] c2=[" + GFPName + "] create");

Let us know if this helps!



Hi Robert,

It works perfectly, thank you.

I suppose the second issue is less of an issue, as I discovered that the subsequent macro triggers remember the location of the folder selected.

1 Like