Problem using exiftool within an Applescript/Automator

Started by agill, March 21, 2014, 06:02:07 PM

Previous topic - Next topic

agill

Hi, I've been using exiftool to copy the contents of a list tag in one file, and put it into the contents of the same tag, and/or another list tag, in a different file.
So on the command line, an example would be:
exiftool -tagsfromfile -IPTC:Keywords Exp0.jpg -XMP-dc:Subject Exp1.jpg
That copies the (IPTC) Keywords from Exp0.jpg into the (XMP) Subject of Exp1.jpg, assuming both JPEGs to be in the same directory.

As I have a lot of these to do, I thought I'd build on the work I'd done before with the Mac's Automator.app (shown here https://exiftool.org/forum/index.php/topic,5651.0.html)
and put an Automator wrapper around it so I didn't have to keep going back to the command line.

However, as far as I can work out, Automator doesn't natively allow input of both a source and destination file, so to enable that I'm running an Applescript within Automator as follows:
on run {input, parameters}
set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE" without invisibles)
set d to quoted form of POSIX path of (choose file with prompt "CHOOSE DESTINATION")
do shell script "exiftool -tagsfromfile -IPTC:Keywords" & s & "-XMP-dc:Subject" & d
return input
end run


When I run this as an app, it carries out the first 4 steps shown below and returns a generic failure like check status and retry, which is of no problem-solving help. By its very nature, I can't run it on the command line, as it's designed to bypass that :)

Running it as a workflow within Automator causes the following:

  • A Finder window comes up requesting the SOURCE
  • I select that and click Choose
  • Another Finder window comes up requesting the DESTINATION
  • I select that and click Choose
  • Automator then returns -
  • Syntax Error
  • No file specified

At the point of returning that error, the "do shell script" line with the exiftool command is highlighted, showing that's where the problem lies. Though it's not clear which file it's referring to, source, destination or both, but the Subject tag in the destination file is not changed, and, thankfully, neither is the Keyword tag in the source file.

Seems I have the syntax incorrect, and just can't seem to see it, despite having played with it for hours, and gone back over the exiftool, Automator and Applescript documentation I used to generate this.

Does anyone have any experience with this to be able to advise or give me some ideas? While I have some familiarity with Automator/Applescript, I'm by no means any kind of expert with either, so remain open to the fact I may be going about it all in an inefficient manner. Perhaps exiftool or Automator can allow both source and destination file parameters I can pass through without me having to use Applescript, though I can't seem to see that.

If anyone can suggest a better way, please feel free, otherwise, corrective suggestions against my incorrect syntax in the Applescript would be very welcome. FYI, I'm using Mac OS X 10.6.8 Snow Leopard.

Thanks in anticipation,
Adrian

Phil Harvey

Hi Adrian,

Quote from: agill on March 21, 2014, 06:02:07 PM
exiftool -tagsfromfile -IPTC:Keywords Exp0.jpg -XMP-dc:Subject Exp1.jpg

That copies the (IPTC) Keywords from Exp0.jpg into the (XMP) Subject of Exp1.jpg, assuming both JPEGs to be in the same directory.

The source file name must come immediately after -tagsFromFile, and the redirection to another tag is done with "<".  So your command should look like this:

exiftool -tagsfromfile Exp0.jpg "-XMP-dc:Subject<IPTC:Keywords" Exp1.jpg

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

agill

Hi Phil,

Thanks for your very prompt response, which shows me that I've still much to learn about exiftool, e.g. where to put the source file definition :)

What I failed to explain in my original post is that I was trying to avoid using the "<" sign as I couldn't work out how to parse it in context.
Applescript requires double quotes around the parts of the command not related to the source/destination files, so translating your example into that context gives me
do shell script "exiftool -tagsfromfile" & s & "-XMP-dc:Subject<IPTC:Keywords" & d

for the new shell script, where everything else is left as before.

But that returns the following Syntax Error
Quotesh: IPTC:Keywords/Users/agill/Pictures/Photos/Canon/201403/Exp1.JPG: No such file or directory
where I think the "<" has acted as a shell redirect because the -XMP-dc:Subject<IPTC:Keywords does not have the required single quotes (single quotes for a Mac) around it.

Certainly it's progress, as it's showing me the problem is with specifying the destination file.

I tried just using single quotes around -XMP-dc:Subject<IPTC:Keywords rather than double quotes, but it returns the Syntax Error
QuoteExpected expression but found unknown token.
As noted, Applescript needs double quotes in this situation.

I then tried using single quotes within double quotes around it as follows
do shell script "exiftool -tagsfromfile" & s & "'-XMP-dc:Subject<IPTC:Keywords'" & d

I figured it most likely wouldn't work, but it seemed worth a try - it took me through the same steps outlined in my original post, with the same error - No file specified.

At this point I'm a bit lost on this, so wonder if you've any suggestions as to how to parse the exiftool requirement for single quotes due to the "<", against Applescript's requirement for double quotes, e.g. is there some kind of "escape" I should be using but have failed to pick up on?

Any thoughts greatly appreciated,
Adrian

agill

Looking more at the documentation etc., I guess I could put the second part of the exiftool command into an arguments file, and have the needed single quotes within that, which may then work.

It's worth a try, so I'll see if I can work it out and post the result accordingly.

agill

Well I seem to have got closer to being able to do what I want, but not close enough.

I used the following AppleScript to enable having the required single quotes around the correct argument in exiftool
on run {input, parameters}
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE" without invisibles)
   set d to quoted form of POSIX path of (choose file with prompt "CHOOSE DESTINATION")
   set exftarg2 to "'-IPTC:Keywords+<=IPTC:Keywords' "
   do shell script "exiftool -tagsfromfile" & s & exftarg2 & d
   return input
end run


This seemed to work as a script because it went though with no reported errors. However, reading the metadata of the destination image after running this script showed that its Keywords had not been set up/added to, and nor had anything else changed that I could see.

Note the space between the single quote and double quote at the end of exftarg2 - if that's not there the workflow/app returns a Syntax Error of
QuoteNo file specified

I guess the above may mean I have the Applescript correct, but don't have the exiftool command correct as it's not doing the requested copy/add.

I also tried it with set exftarg2 to "'-IPTC:Keywords<IPTC:Keywords' "
which had the same effect, the script ran with no reported errors, but there was no change to the metadata of the destination file, so the Keywords were not put in from the source file.

I realize that copying Keywords to Keywords does not need the repetition of the tag name, but I was also substituting in XMP-dc:Subject, and left it all in that format to help with problem resolution.

Just for completeness, the effect is the same using set exftarg2 to either "'-XMP-dc:Subject<IPTC:Keywords' " or set exftarg2 to "'-XMP-dc:Subject+<=IPTC:Keywords' "
- there was still no change to the metadata of the destination file, so the Subject was not populated with the Keywords from the source file.

For further completeness I tried all of the above with a space between the leading double and single quotes of exftarg2 - same effect, nothing changed in the destination file metadata, though the script ran with no reported errors.

If anyone can see what I'm doing wrong, and let me know, I'd be very grateful.
Thanks, Adrian

Phil Harvey

Hi Adrian,

Don't you need spaces between the arguments and the option on the command line?: "exiftool -tagsfromfile " & s & " " & ...
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

agill

Hi Phil,

You do indeed need spaces between the arguments and options - and I thought I'd done that by building the spaces into the argument text and/or file. But I'd tried SO many variations I was thoroughly lost and couldn't see the proverbial "wood for the trees"!

THANK YOU for your timely reminder about it as that sent me back to the whole thing with fresh perspective, and now it works! :)

The code in it's current working state is as follows:
on run {input, parameters}
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE" without invisibles)
   set d to quoted form of POSIX path of (choose file with prompt "CHOOSE DESTINATION")
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-XMP-dc:Subject<IPTC:Keywords' "
   do shell script "exiftool" & exftarg1 & s & exftarg2 & d
   return input
end run


Note the:

  • space before and after " -tagsfromfile " in the arguments file exftarg1
  • space between the double and single quotes at either end of the contents of exftarg2.
Without all of those spaces as shown, the exiftool command will not be carried out, even if the script around it runs without apparent error.

I used variables in the script to declare all of the exiftool arguments as argument files so I can more easily change what I'm doing with the images. But that's not strictly necessary as argument text directly embedded into the shell script will be accepted, e.g.
on run {input, parameters}
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE" without invisibles)
   set d to quoted form of POSIX path of (choose file with prompt "CHOOSE DESTINATION")
   do shell script "exiftool -tagsfromfile " & s & " '-XMP-dc:Subject<IPTC:Keywords' " & d
   return input
end run

This ran with no problems and made the required changes.

You can also combine embedded text and argument files, e.g.
on run {input, parameters}
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE" without invisibles)
   set d to quoted form of POSIX path of (choose file with prompt "CHOOSE DESTINATION")
   set exftarg2 to " '-XMP-dc:Subject<IPTC:Keywords' "
   do shell script "exiftool -tagsfromfile "  & s & exftarg2 & d
   return input


Thanks as ever Phil for your prompt and insightful assistance - greatly appreciated,
Adrian

bestsides

omg! this helped me so much. Newbie to exiftools, limited knowlege w/automator, desperately trying to learn and understand scripting. This post and the work shared by agill & Phil - big thank you to both. I was able to modify the script to take the CreateDate from one image to copy over to another image that should have had that same CreateDate but didn't - probably do to a prior restore or migration. (note: I can do this from the command line but really want to make automator apps so I can simplify - for me - and stay out of the terminal command line when possible). I have quite a few of these files so I need to figure out how to tweak this script to take the CreateDate from a single source file and copy it to several destination png graphic images in the same folder created at the same time but all with the wrong date.