-TAG-= Conditional Tag Testing Revisited [Example is in an -@ ARGFILE context]

Started by lewisn, April 06, 2016, 06:40:11 PM

Previous topic - Next topic

lewisn

I had the following lines in an -@ ARGFILE.  This worked nicely to update caption info in IPTC:Caption-Abstract, updating it if need be in the following priority order: IPTC:Caption-Abstract, EXIF:ImageDescription, XMP:Title, EXIF:XPTitle. 

-IPTC:Caption-Abstract<EXIF:XPTitle
-IPTC:Caption-Abstract<XMP:Title
-IPTC:Caption-Abstract<EXIF:ImageDescription
-IPTC:Caption-Abstract<IPTC:Caption-Abstract


The above works because IPTC:Caption-Abstract always refers to it's original value in the source file, so the last line restores it if it already exists; otherwise the tag is left updated in priority order.

The downside with the above was that if any of the four fields exist the file will get rewritten even if Caption-Abstract already exists.  So I tired the following, adding tests to only do the copies if IPTC:Caption-Abstract doen't exist, making the assumption that IPTC:Caption-Abstract on the left when testing also always refers to the original value.  However I couldn't get this to work, and after playing with it a bit I think the later assumption is not correct.

-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<EXIF:XPTitle
-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<XMP:Title
-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<EXIF:ImageDescription
-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<IPTC:Caption-Abstract


I've used this test syntax in my ARGFILE for multiple independent tag tests and tag updates before, but apparently if I try to stack them in the manner I have and make the assumption that a tag in the condition on the left always refers to the original file value as well, it fails.

At this point I've decided to either use -if conditions coupled with -execute commands to get the greater flexibility that -if statements provide with Perl syntax, or to go directly to the use of a user-defined tag which, in examples I've seen on the forum, is even more succinct, efficient, and convenient.  However, I thought I'd put this out there anyway to see if I've missed something and better clarify the behavior in case someone else wants to try something like the above.

Phil Harvey

I don't know what you're trying to do with your second argfile, but doesn't this do what you want?:

-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<EXIF:XPTitle
-IPTC:Caption-Abstract<XMP:Title
-IPTC:Caption-Abstract<EXIF:ImageDescription


Here, the file should not be rewritten if Caption-Abstract already exists.

- Phil

Edit: Alternatively, the -wm option could be used to avoid writing existing tags, and this should have the same effect:

-wm
cg
-IPTC:Caption-Abstract<EXIF:XPTitle
-IPTC:Caption-Abstract<XMP:Title
-IPTC:Caption-Abstract<EXIF:ImageDescription
...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 ($).

lewisn

Oh, Those are good.  The light is coming on somewhat in how -TAG-= conditional testing works.  I had the impression that such tag conditions matched up with corresponding following -TAG assignment lines, so in my example in adding the conditional tests I paired a test with each of the subsequent assignments.  I see, rather, that the test applies to all subsequent matching TAG assignments, the last one executed of course taking precedence.   That is good.   Thanks also for your additional example in use of the -wm (-writeMode) option.

-LewisN

lewisn

I thought I'd share an update.

Thanks to Phil I tried the following which worked well:

-IPTC:Caption-Abstract-=
-addtagsfromfile
@
-IPTC:Caption-Abstract<EXIF:XPTitle
-IPTC:Caption-Abstract<XMP:Title
-IPTC:Caption-Abstract<EXIF:ImageDescription
-IPTC:Caption-Abstract<IPTC:Caption-Abstract


I then decided I wanted to incorporate checks for fields that might be empty or have whitespace in them, so I replaced the above with a call to a user defined tag that incorporated the regular expression syntax shared by Hayo in response to another post I made here (See: https://exiftool.org/forum/index.php/topic,7158.0.html).   

Here's my user defined tag:


PrioritizedCollectedCaption => {
    Desire => {
        0 => 'IPTC:Caption-Abstract',
        1 => 'EXIF:ImageDescription',
        2 => 'XMP:Title',
        3 => 'EXIF:XPTitle',
    },
    ValueConv => q{
        return undef if $val[0] and $val[0] !~ /\A\s*\z/s;  # is ok, do not write file
        return $val[1] if $val[1] and $val[1] !~ /\A\s*\z/s;
        return $val[2] if $val[2] and $val[2] !~ /\A\s*\z/s;
        return $val[3] if $val[3] and $val[3] !~ /\A\s*\z/s;
        return '' if $val[0] and $val[0] =~ /\A\s*\z/s;
        return undef;  #you get here if $val[0]='' and other tests found nothing
          #Notes: 'return undef' causes exiftool to not update tag
          #       'if $val[xx]' checks that tag exists and that it is not "" or "0"
    },
},


I then call it from my -@ ARGFILE script as follows:

-IPTC:Caption-Abstract<PrioritizedCollectedCaption
   # The user-defined tag picks up caption info in IPTC:Caption-Abstract, EXIF:ImageDescription,
   # XMP:Title, EXIF:XPTitle priority order. Tags with only whitespace are not copied, except in
   # the case of Caption-Abstract which will be reduced to "" if no other valid tags are present.


I then follow this up with another exiftool pass to remove any empty IPTC:Caption-Abstract tags that might be left behind by adding this at the end of my -@ ARGFILE script:

-execute
-echo
Now deleting any empty ("") Caption-Abstract tags:
-IPTC:Caption-Abstract-=


This final operation cannot be in the same pass as it will interfere with the previous operation.   I don't believe there is a way to force a delete of a tag from within a user defined tag, so the best I could do was reduce any final Caption-Abstract tag remaining with only white space in it to "", which I then delete in this separate pass.   

I appreciate the assistance of Phil, Hayo, and others.  Unless I've missed something in my testing the above works well and I'm happy with it.  Of course any additional comments or suggestions would be appreciated.

LewisN

Phil Harvey

Hi Lewis,

This may take some thought, but I'm away on vacation for a week and am in relax mode right now.  But I'll think about this as soon as I get a chance.

- 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 ($).