problem escaping in exiftool using exec() macro command on Windows

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

problem escaping in exiftool using exec() macro command on Windows

Bill Christens-Barry-2
Using ImageJ 1.53c and earlier, I'd like to be able to use the exec() command and exiftool on a Windows 10 machine to assign the contents of a text file to a particular metadata tag (Image Description) of an image.

On macOS using exiftool the following macro command works fine using backslash escaping of the arguments to exiftool (warning: this line may have been broken into two lines in this posting):

    exec("bash", "-c", "/usr/local/bin/exiftool \"-ImageDescription<=/Volumes/USBstick1/ExampleText.txt\" /Volumes/USBstick1/ExampleImage.tif");

On a macOS machine this correctly writes the text in the file "ExampleText.txt" file into the Image Description metadata tag in the header of "ExampleImage.tif". Exiftool documentation notes that for this operation the "<" character needs to be escaped, as is done here using backslash quote escaping. It's also OK to use "ImageDescription" without a space in the exiftool command.

On Windows I've not been able to make this work. The problem seems to be determining how to escape the special character "<" in the arguments to exiftool. After much searching and many attempts to escape via the usual escape suspects (`, \, ^, etc.) I haven't yet solved this.

The syntax of the call to exiftool itself, i.e.:

    exec("cmd", "/c", "C:Windows/exiftool.exe", "xxxx xxxx");

is successful, as using a similar construct to read the ImageDescription tag works. However, the details of escaping the arguments to exiftool (shown as "xxxx xxxx" here) that must be done to write that tag due to the "<" character seems to be the problem.

Does anyone have suggestions or an example of how this can be done?

Thanks.

Bill

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: problem escaping in exiftool using exec() macro command on Windows

Gabriel Landini
Hi Bill,
Have you tried escaping the "\" as well? i.e. "\\^" or "\\\^" depending of
what what the command expects (which I am not very clear either).

Cheers

Gabriel

On Saturday, 15 August 2020 15:56:20 BST [hidden email] wrote:

> Using ImageJ 1.53c and earlier, I'd like to be able to use the exec()
> command and exiftool on a Windows 10 machine to assign the contents of a
> text file to a particular metadata tag (Image Description) of an image.
>
> On macOS using exiftool the following macro command works fine using
> backslash escaping of the arguments to exiftool (warning: this line may
> have been broken into two lines in this posting):
>
>     exec("bash", "-c", "/usr/local/bin/exiftool
> \"-ImageDescription<=/Volumes/USBstick1/ExampleText.txt\"
> /Volumes/USBstick1/ExampleImage.tif");
>
> On a macOS machine this correctly writes the text in the file
> "ExampleText.txt" file into the Image Description metadata tag in the
> header of "ExampleImage.tif". Exiftool documentation notes that for this
> operation the "<" character needs to be escaped, as is done here using
> backslash quote escaping. It's also OK to use "ImageDescription" without a
> space in the exiftool command.
>
> On Windows I've not been able to make this work. The problem seems to be
> determining how to escape the special character "<" in the arguments to
> exiftool. After much searching and many attempts to escape via the usual
> escape suspects (`, \, ^, etc.) I haven't yet solved this.
>
> The syntax of the call to exiftool itself, i.e.:
>
>     exec("cmd", "/c", "C:Windows/exiftool.exe", "xxxx xxxx");
>
> is successful, as using a similar construct to read the ImageDescription tag
> works. However, the details of escaping the arguments to exiftool (shown as
> "xxxx xxxx" here) that must be done to write that tag due to the "<"
> character seems to be the problem.
>
> Does anyone have suggestions or an example of how this can be done?
>
> Thanks.
>
> Bill
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: problem escaping in exiftool using exec() macro command on Windows

Bill Christens-Barry-2
In reply to this post by Bill Christens-Barry-2
Hi Gabriel,

Thanks for your thoughts and suggestions on this. Pulling out my hair I've tried a variety of seemingly rational and sometimes random numbers and combinations of the usual escape characters, none of which has solved this problem. Single and multiple \ and ^ characters seem not to work, and I'll try your further suggestion using multiple backslashes with a ^. In some cases it's as though the escape characters have a persistent effect through the remainder of the macro command, evidence of which is that the intended following path argument (beginning with "C:/) is pointed to in the error message. But I've confirmed through testing that this alone isn't the problem, as I can read a tag using the same absolute paths.

I've read online (e.g. https://stackoverflow.com/questions/42032953/which-symbol-is-escape-character-in-cmd) that cmd is not consistent across all cases and that there is the further issue of how escaping is handled further downstream. I suppose it may be time for me to look into PowerShell.

Thanks and be well,

Bill

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: problem escaping in exiftool using exec() macro command on Windows

Fred Damen
In reply to this post by Bill Christens-Barry-2
Greetings,

Short answer:
You do not need to escape the '<' character; what you need to do is call
the exiftool more directly, {i.e. exec("C:Windows/exiftool.exe",
"arg1","arg2");}, where only the IJ macro interpreter escaping needs to
occur. Note that you need to pass separate arguments to exiftool as
separate strings.

Long answer:
Strings in the situation you describe will be interpreted several times.

First by the compiler/interpreter. To determine what is going on here
replace the 'exec' subroutine call by the language's equivalent of C's
printf, and examine the output. The 'exec' *system* subroutine will
execute the exact command identified in the first argument and then just
pass the strings as an array, i.e., in C lingo 'char** argv', but no
modifications of the strings will occur.

This command, due to laziness and/or flexibility, is usually a shell
(windows:"cmd").  To see what is passed to the shell you can use the
system 'echo' command instead of the shell; note this must be the exact
command path to the system 'echo' command, as the shell does the path
searching stuff.  The shell will interpret the array of strings as
arguments, where "/c" says the the next string argument is the command
line to run.  The shell interprets this string to figure out what to setup
and what to pass to the system 'exec' subroutine; where part of this setup
is escaping special characters, or processing them (e.g. '<' means open
the following file name as 'stdin'). To see the arguments the shell is
passing to the command that is subsequently run by the system 'exec'
subroutine, replace the first blank delimited word of the command line
with the system 'echo' command.  Note that there are shell 'echo'
statements, do not use them.

So, unless you need to use the services of the platform's shell, then just
run the command directly.  When I am setting this up in one of my
programs, I usually use the system echo command until I am convinced that
all the string manipulations are handled correctly.

Enjoy,

Fred

In shell:
windows: where echo
unix: which echo


On Sat, August 15, 2020 9:56 am, Bill Christens-Barry wrote:

> Using ImageJ 1.53c and earlier, I'd like to be able to use the exec()
> command and exiftool on a Windows 10 machine to assign the contents of a
> text file to a particular metadata tag (Image Description) of an image.
>
> On macOS using exiftool the following macro command works fine using
> backslash escaping of the arguments to exiftool (warning: this line may
> have been broken into two lines in this posting):
>
>     exec("bash", "-c", "/usr/local/bin/exiftool
> \"-ImageDescription<=/Volumes/USBstick1/ExampleText.txt\"
> /Volumes/USBstick1/ExampleImage.tif");
>
> On a macOS machine this correctly writes the text in the file
> "ExampleText.txt" file into the Image Description metadata tag in the
> header of "ExampleImage.tif". Exiftool documentation notes that for this
> operation the "<" character needs to be escaped, as is done here using
> backslash quote escaping. It's also OK to use "ImageDescription" without a
> space in the exiftool command.
>
> On Windows I've not been able to make this work. The problem seems to be
> determining how to escape the special character "<" in the arguments to
> exiftool. After much searching and many attempts to escape via the usual
> escape suspects (`, \, ^, etc.) I haven't yet solved this.
>
> The syntax of the call to exiftool itself, i.e.:
>
>     exec("cmd", "/c", "C:Windows/exiftool.exe", "xxxx xxxx");
>
> is successful, as using a similar construct to read the ImageDescription
> tag works. However, the details of escaping the arguments to exiftool
> (shown as "xxxx xxxx" here) that must be done to write that tag due to the
> "<" character seems to be the problem.
>
> Does anyone have suggestions or an example of how this can be done?
>
> Thanks.
>
> Bill
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: problem escaping in exiftool using exec() macro command on Windows

Bill Christens-Barry-2
In reply to this post by Bill Christens-Barry-2
Fred,

Thanks for your detailed analysis and explanation of how what I've construed as a bucket brigade actually operates. There's a lot for me to ponder and absorb in your comments - much appreciated. Before reading this I had wandered into the ".cmd" approach in an attempt to bypass some of my escapist efforts, and will now try to take advantage of your response.

Bill

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html