Writing hierarchical tags in alphabetical order (hierarchicalSubject)?

Started by mlaverdiere, May 09, 2018, 10:53:53 AM

Previous topic - Next topic

mlaverdiere

Hi,

I have a (quite complex) script for applying various exiftool passes on my photo library, mainly in order to deal with hierarchicalSubject tags in xmp and image (jpg/nef/etc.) files.

From what I read, I understand that exiftool should write these tags in alphabetical order, but this is not what I get and it causes a little glitch in my workflow with darktable (I wouldn't care too much otherwise that the tags are not listed in alphabetical order).

Here are the main exiftool commands in my script (see here for more details on the reason behind these commands: https://exiftool.org/forum/index.php/topic,9125.0.html):

#To separate hierarchical tags
exiftool -m -srcfile %d%f.%e -@ file.txt -overwrite_original -if '$hierarchicalsubject ne $XMP-mediapro:UserFields' '-hierarchicalsubject<${hierarchicalsubject;my @a=split /--/;my @b=@a;my $n=@b;for (;;) { foreach (@a) { push @b, $_ if s/\|[^|]+$// } last if $n==@b; $n = @b} $_=join "--",@b}' -sep '--'

#To remove duplicates
exiftool -m -srcfile %d%f.%e -@ file.txt -overwrite_original -if '$hierarchicalsubject ne $XMP-mediapro:UserFields' -sep '##' '-hierarchicalSubject<${hierarchicalSubject;NoDups}'

And here's an example of what I get in the generated xmp file, where tags not in alphabetical order:

<lr:hierarchicalSubject>
   <rdf:Bag>
    <rdf:li>AAA</rdf:li>
    <rdf:li>BBB|CCC</rdf:li>
    <rdf:li>MMM|PPP</rdf:li>
    <rdf:li>YYY</rdf:li>
    <rdf:li>ZZZ</rdf:li>
    <rdf:li>BBB</rdf:li>
    <rdf:li>MMM</rdf:li>
   </rdf:Bag>
  </lr:hierarchicalSubject>

Is there any way to generate xmp files with tags listed in alphabetical order?

Thanks for any help.

Phil Harvey

ExifTool writes XMP tags in alphabetical order of tag name.  List values for a given tag are written in the order they are entered.

To sort the list entries in alphabetical order, you could modify your first command by adding a "sort", like this:

exiftool -m -srcfile %d%f.%e -@ file.txt -overwrite_original -if '$hierarchicalsubject ne $XMP-mediapro:UserFields' '-hierarchicalsubject<${hierarchicalsubject;my @a=split /--/;my @b=@a;my $n=@b;for (;;) { foreach (@a) { push @b, $_ if s/\|[^|]+$// } last if $n==@b; $n = @b} $_=join "--",sort @b}' -sep '--'

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