Further Problems using exiftool within an Applescript/Automator - NOW SOLVED!

Started by agill, March 24, 2014, 09:51:52 PM

Previous topic - Next topic

agill

Goal
To use Automator/Applescript as a GUI to enable exiftool to take tag data from a source file and output it to multiple destination files in the same or different tags as needed.

PROBLEM RESOLVED USING APPLESCRIPT - specifically the third example in the Applescript section below, which was not correct because I thought the results of the repeat loop were in fileList, when actually they're in shellParameters - I still have a lot to learn about Applescript! Someone on the Macscripter forum pointed out my mistake.

THE CORRECT Applescript CODE - to enable the contents of a list source file tag to be written to specified tags in multiple destination files is -
on run {input, parameters}
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
   -- set d to quoted form of POSIX path of (choose file with prompt "CHOOSE FILES TO CHANGE" with multiple selections allowed)
   set chosenFiles to (choose file with prompt "CHOOSE FILE TO CHANGE" with multiple selections allowed)
   set fileList to {}
   repeat with aFile in chosenFiles
      -- coerce the alias to POSIX path and quote it
      set end of fileList to quoted form of POSIX path of aFile
   end repeat
   set {TID, text item delimiters} to {text item delimiters, space}
   -- converts the list to a plain string space separated
   set shellParameters to fileList as text
   set text item delimiters to TID
   do shell script "exiftool" & exftarg1 & s & exftarg2 & shellParameters
   return input
end run


That worked first time on multiple files! It's late here now, so I can't get to any more testing, but I at least wanted to get this out tonight so that no-one need reply.

I'm leaving the rest of the original text in place for others to see how I worked it all out, and in case anyone wants to comment on doing this a better way. For me the Applescript is perhaps the neatest way, even if it proved the most complex to work out.

Trying to Create a Text File with exiftool
Can exiftool output a plain text file, e.g. can it take the IPTC:Keywords, and/or the XMP-dc:Subject, and put them into a text file? If it can, I could simply use Automator actions to take the contents of these list tags and put them into a text file, and from that text file I can populate any other images with the same data, again using Automator actions. That would be a good workaround, and while it involves more steps, would avoid the pain of going through what I'm currently doing with Applescript as outlined below.

I did try to output a text file, but received a message that the format (.txt) is not supported, but if there's a way to do it, or via another format (CSV for example?) it's perhaps the easiest for me to get working. Any guidance on formatting the exiftool command to do that, would be greatly appreciated. I assume once I have such a file I could use something like
exiftool '-XMP-dc:Subject+<=/Users/agill/Pictures/Photos/KeywSubj.txt' Exp1.jpg
To get the data from the text (or other format) file into the image - the example uses just one image on the command line, but I can use Automator to make it multiple images.

I looked at outputting CSV, but wasn't sure if I'd get a format I could then use to bring back into images - like what happened with XMP below.

Creating an XMP file
Using -tagsfromfile I successfully created an XMP file, but found I could not use it to populate the XMP-dc:Subject of any other image. Again if there's a way to create an XMP file I can use to populate the Subject of other files, that would save me a lot of work with Applescript.
When trying to use a .xmp output file to write the XMP-dc:Subject as follows
exiftool '-XMP-dc:Subject+<=/Users/agill/Pictures/Photos/KeywSubj.xmp' Exp1.jpg
the following data is put into that tag in the destination image -
<?xpacket begin='Ôªø' id='W5M0MpCehiHzreSzNTczkc9d'?>.<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Image::ExifTool 9.54'>.<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>.. <rdf:Description rdf:about=''.  xmlns:dc='http://purl.org/dc/elements/1.1/'>.  <dc:subject>.   <rdf:Bag>.    <rdf:li>wildlife</rdf:li>.    <rdf:li>garden</rdf:li>.    <rdf:li>squirrels</rdf:li>.   </rdf:Bag>.  </dc:subject>. </rdf:Description>.</rdf:RDF>.</x:xmpmeta>.<?xpacket end='w'?>

What I was trying to do was just have "wildlife", "garden" and "squirrels" as keywords in the XMP-dc:Subject of the destination file.

Continuing with Applescript
With help from Phil I was able to create an Automator/Applescript GUI to enable exiftool to take tag data from a source image and output it to another image - https://exiftool.org/forum/index.php/topic,5685.0.html

I'm now trying to make the Applescript work with exiftool's ability to output to multiple destination files, rather than just one.

My first attempt was
on run {input, parameters}
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
   set d to quoted form of POSIX path of (choose file with prompt "CHOOSE FILES TO CHANGE" with multiple selections allowed)
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
   do shell script "exiftool" & exftarg1 & s & exftarg2 & d
   return input
end run

The difference between this and what I originally had working is the addition of "with multiple selections allowed" on the "set d" syntax. But it only executed exiftool correctly if one image destination file was selected even though it allowed selection of multiple destination files. If multiple files were selected, it returned the following
QuoteSyntax Error
Can't get POSIX path of {alias "AGMBA32_SSD:Users:agill:Pictures:Photos:2007:200707_squirrel:IMGP0002.JPG", alias "AGMBA32_SSD:Users:agill:Pictures:Photos:2007:200707_squirrel:IMGP0003.JPG"}.
I read around some Applescript forums and documentation and adjusted the script as follows
on run {input, parameters}
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
   set chosenFiles to (choose file with prompt "CHOOSE FILE TO CHANGE" with multiple selections allowed)
   set fileList to {}
   repeat with aFile in chosenFiles
      -- coerce the alias to POSIX path and quote it
      set end of fileList to quoted form of POSIX path of aFile
   end repeat
   set d to ""
   repeat with anItem in fileList
      set d to d & space & quoted form of (anItem's |path|() as text)
   end repeat
   do shell script "exiftool" & exftarg1 & s & exftarg2 & d
   return input
end run


This was an attempt to ensure what was passed to exiftool was in a format it can read, i.e. paths to files as text separated by space. That returned
QuoteSyntax Error
item 1 of {"'/Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0002.JPG'", "'/Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0003.JPG'"} doesn't understand the path message.

Figuring exiftool may not like that way of making posix paths into text separated by spaces, I also tried
on run {input, parameters}
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
   set chosenFiles to (choose file with prompt "CHOOSE FILE TO CHANGE" with multiple selections allowed)
   set fileList to {}
   repeat with aFile in chosenFiles
      -- coerce the alias to POSIX path and quote it
      set end of fileList to quoted form of POSIX path of aFile
   end repeat
   set {TID, text item delimiters} to {text item delimiters, space}
   -- converts the list to a plain string space separated
   set shellParameters to fileList as text
   set text item delimiters to TID
   do shell script "exiftool" & exftarg1 & s & exftarg2 & fileList
   return input
end run

which returned
QuoteWarning: Invalid PrintIM header - /Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0001.JPG
Error: File not found - /Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0002.JPG/Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0003.JPG
apparently concatenating the file names of the 2 files I'd selected into one long name.

At that point I thought maybe I don't need to change it to text from a posix path, so tried
on run {input, parameters}
   set exftarg1 to " -tagsfromfile "
   set exftarg2 to " '-IPTC:Keywords<IPTC:Keywords' '-XMP-dc:Subject<IPTC:Keywords' "
   set s to quoted form of POSIX path of (choose file with prompt "CHOOSE SOURCE FILE" without invisibles)
   set chosenFiles to (choose file with prompt "CHOOSE FILE TO CHANGE" with multiple selections allowed)
   set fileList to {}
   repeat with aFile in chosenFiles
      -- coerce the alias to POSIX path and quote it
      set end of fileList to quoted form of POSIX path of aFile
   end repeat
   do shell script "exiftool" & exftarg1 & s & exftarg2 & fileList
   return input
end run


That returned
QuoteSyntax Error Warning: Invalid PrintIM header - /Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0001.JPG
Error: File not found - /Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0002.JPG/Users/agill/Pictures/Photos/2007/200707_squirrel/IMGP0003.JPG
again apparently concatenating the file names as one long one.

The problem seems to be at the interface between Applescript and exiftool. The above attempts typically work when selecting just one destination file, but not when selecting multiple files. Which leads me to ask what format does exiftool expect for multiple file names to be passed to it to then be able to act on all of them? Text delimited by space doesn't seem to work, and nor do posix paths as long as the above attempts are generating what I believe they are - have done my best to check via Applescript forums and documentation.

I'm no Applescript guru, so could simply be making syntax mistakes, and I do keep checking for those. Assuming I either have, or eventually do get, the Applescript syntax right, I'll need to correctly define the multiple destination files/paths to pass to exiftool anyway, so it would help to know that - my apologies if it's documented somewhere that I missed!

Thanks as always for all help given,
Adrian

Phil Harvey

Hi Adrian,

It is the command shell, not exiftool, which parses the command line.

File names must be properly escaped, and multiple file names must be separated by spaces.

- 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

Thanks Phil for letting me know it's the shell rather than exiftool that parses the command line as that helps my understanding of it all.
I was getting a bit lost at times trying to work with exiftool, Applescript, and Automator all at once. But folks such as yourself have been very helpful in explaining the relevant parts, which means i was able to get it all working, learning a lot in the process.

With your explanation it's easier to see why when I used the script to set up shellParameters as text output, with the path/file names separated by spaces, it worked.

My thanks again for your continued help and support, along with the folks on the Macsripter forum,
Adrian