Dealing with spaces in a directory path in a macro that runs exiftool

Hi -

I am writing a macro and at the end I want to run exiftool to copy metadata from one image to another. I have it working but a colleague asked me to make it work with spaces in directory paths and now I’m stuck. This is the line giving me problems:

exec(exiftoolDirectory + "exiftool -tagsfromfile "  + "\"" + inDirectory + filesToProcess[i] + "\"" + " " + "\"" + outDirectory + outputFileName + "\"");

The exiftool needs double-quotes if the path has spaces. The string created within the exec function is:

exiftool -tagsfromfile "/media/nedhorning/684EE5FF4EE5C642/AMNH/PhotoMonitoring/Vignetting/test image/2016_0401_105800_094.JPG" "/media/nedhorning/684EE5FF4EE5C642/AMNH/PhotoMonitoring/Vignetting/output image/2016_0401_105800_094.jpg"

When I run this on the command line in Ubuntu it works just fine. So to me it seems like the string concatenation in the exec() function is formed properly for exiftool but something must be happening before the command is passed by ImageJ to the system.

Any help would be appreciated.


Eventually the space is interpreted by Runtime.exec as an argument.

Try the following suggestions:

You can also call the bash and execute the command in the bash, see:

Thanks for the reply Bio7. I tried putting an array of strings in exec() based on my interpretation of the stackoverflow suggestion. My change looks like this:

string1 = exiftoolDirectory + "exiftool";
string2 = "-tagsfromfile";
string3 = "\"" + inDirectory + filesToProcess[i] + "\"";
string4 = "\"" + outDirectory + outputFileName + "\"";
exifStringArray = newArray(string1, string2, string3, string4);

I got an error “Number or numeric function expected”

I like the option to execute the command in the bash but am not sure how to distinguish between Mac, Linux, Windows in the macro so I run the proper shell.

You can get the OS inside the macro with:

//Returns the OS name ("Mac OS X", "Linux" or "Windows").

For more see:

Also have a look at some suggestions here:

It can be a little bit tricky.

Here again for completeness the macro API description of exec:

Thanks for the getInfo() details. I have an old macro guide and that option isn’t in it. For now I have the macro working by identifying the OS then using the appropriate shell command. I’d still like to figure out how to do it without that step but at least it’s working. I definitely agree that it can be a little bit tricky. Thanks again for your help. Ned