ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: bigfire60 on March 16, 2020, 11:15:21 AM

Title: How to parse list fields
Post by: bigfire60 on March 16, 2020, 11:15:21 AM
Searched the forum and online but couldn't find an example to parse through list values.
Basically, I'd like to read each value in the Microsoft People schema XMP-MP-RegionPersonDisplayName
and add it to XMP-dc:subject and IPTC-keywords if they don't currently exist there.

Any advice would be appreciated.
Title: Re: How to parse list fields
Post by: Phil Harvey on March 16, 2020, 11:21:15 AM
Try this:

exiftool -addtagsfromfile @ "-xmp-dc:subject-<XMP-MP:RegionPersonDisplayName" "-xmp-dc:subject+<XMP-MP:RegionPersonDisplayName" "-iptc:keywords-<XMP-MP:RegionPersonDisplayName" "-iptc:keywords+<XMP-MP:RegionPersonDisplayName" DIR

FAQ 17 (https://exiftool.org/faq.html#Q17) describes the technique I used here to avoid duplicate entries.

- Phil
Title: Re: How to parse list fields
Post by: bigfire60 on March 16, 2020, 11:32:49 AM
You are the master!
Thanks for this wonderful piece of software...
Title: Re: How to parse list fields
Post by: bigfire60 on March 16, 2020, 11:47:35 AM
One minor issue, keywords are duplicated if they previously existed.
Title: Re: How to parse list fields
Post by: Phil Harvey on March 16, 2020, 11:49:46 AM
Shouldn't be
Title: Re: How to parse list fields
Post by: bigfire60 on March 16, 2020, 11:53:55 AM
Right again, it was a typo!
Title: Re: How to parse list fields
Post by: bigfire60 on March 20, 2020, 08:06:20 AM
Hi Phil,
I have a follow-up question.
I'm sure this can be done programmatically, but it may be too complex for the command line / batch file.
I want to check each person name in XMP-MP:RegionPersonDisplayName list for a matching value in IPTC:keywords.
Output the person name if condition is true (no matching name).

Thanks.
Title: Re: How to parse list fields
Post by: Phil Harvey on March 20, 2020, 09:18:03 AM
That can be done with this command:

exiftool -config missingnames.config -missingnames DIR

and this config file:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MissingNames => {
            Require => {
                0 => 'XMP-MP:RegionPersonDisplayName',
            },
            Desire => {
                1 => 'IPTC:Keywords',
            },
            ValueConv => q{
                my @people = ref $val[0] ? @{$val[0]} : ( $val[0] );
                my @keywords;
                @keywords = ref $val[1] ? @{$val[1]} : ( $val[1] ) if defined $val[1];
                my %keys;
                $keys{$_} = 1 foreach @keywords;
                my @out;
                $keys{$_} or push @out, $_ foreach @people;
                return @out ? \@out : undef;
            },
        },
    },
);
1;  #end


- Phil
Title: Re: How to parse list fields
Post by: bigfire60 on March 20, 2020, 12:13:17 PM
Thanks so much.
Worked like a charm.
Didn't even consider config files.
What programming language do they use?

Title: Re: How to parse list fields
Post by: StarGeek on March 20, 2020, 12:31:16 PM
Quote from: bigfire60 on March 20, 2020, 12:13:17 PM
What programming language do they use?

Exiftool and the config files are Perl programs.  Even the Windows executable is just PAR packed Perl code (https://en.wikipedia.org/wiki/Perl_Archive_Toolkit).
Title: Re: How to parse list fields
Post by: bigfire60 on March 20, 2020, 12:58:02 PM
Very good, thanks.
Title: Re: How to parse list fields
Post by: bigfire60 on March 21, 2020, 08:30:52 AM
Unfortunately, I don't know Perl.
I took a stab but couldn't figure out how to also output the filename when the condition is true.
Could you be so kind?
Title: Re: How to parse list fields
Post by: StarGeek on March 21, 2020, 10:57:39 AM
Just use the same config file
exiftool -config missingnames.config -if "$missingnames" -Filename  <FileOrDir>
Title: Re: How to parse list fields
Post by: bigfire60 on March 21, 2020, 08:06:45 PM
Now it will output the filename, but not the missing names.

The attached screen capture show the initial run without the if condition
where the missing names appear. The last run shows the filename but not the missing name.

Title: Re: How to parse list fields
Post by: bigfire60 on March 21, 2020, 08:23:57 PM
Oops, just had to add the -missingnames to the list.

Is there a way to suppress the linefeed between Filename & missing name?
Title: Re: How to parse list fields
Post by: StarGeek on March 21, 2020, 09:30:26 PM
See the -p (printFormat) option (https://exiftool.org/exiftool_pod.html#p-FMTFILE-or-STR--printFormat) to format the output as you would like.
Title: Re: How to parse list fields
Post by: bigfire60 on March 22, 2020, 11:31:16 AM
Perfect.
Thanks StarGeek.