Alphabetically ordering list type tags?

Started by marcobarrios, August 12, 2018, 11:40:00 AM

Previous topic - Next topic

marcobarrios

Is there a way to alphabetically sort or order the written items in a list type tag?

For example the command exiftool -keywords="Japan" -keywords="Andorra" -keywords="Island" does not alphabetically order the different keywords. This command will results the keywords tag to have the value "Japan, Andorra, Island". Same is true if a keyword is later added with the command exiftool -keywords+="Armenia".

Any ideas how to sort the items of list type tag? Thank you in advance!

StarGeek

First of all, is there a specific reason that you want this?  Or do you want it because you feel it looks better?

I ask because I have felt the desire to do so in the past, but forced myself not to care, as it doesn't add anything useful for the time it takes to do so.

But to answer your question, you can't do so while adding items to the list.  But you can sort it afterwards by using a command like this.  I'm assuming you want a case-insensitive sort

exiftool -Sep "##" "-Keywords<${Keywords;$_=join '##',(sort{lc($a) cmp lc($b)} split '##')}" FileOrDir

Example output:
C:\>exiftool -g1 -a -s -iptc:keywords y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : D, A, B, C, Birthday, ape, donkey, crocodile, dog

C:\>exiftool -Sep "##" "-Keywords<${Keywords;$_=join '##',(sort{lc($a) cmp lc($b)} split '##')}" y:\!temp\Test3.jpg
    1 image files updated

C:\>exiftool -g1 -a -s -iptc:keywords y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : A, ape, B, Birthday, C, crocodile, D, dog, donkey


A case sensitive sort would be like this:
exiftool -Sep "##" "-Keywords<${Keywords;$_=join '##',(sort split '##')}" FileOrDir

Output:
C:\>exiftool -g1 -a -s -iptc:keywords y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : D, A, B, C, Birthday, ape, donkey, crocodile, dog

C:\>exiftool -Sep "##" "-Keywords<${Keywords;$_=join '##',(sort split '##')}" y:\!temp\Test3.jpg
    1 image files updated

C:\>exiftool -g1 -a -s -iptc:keywords y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : A, B, Birthday, C, D, ape, crocodile, dog, donkey
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

marcobarrios

Thank you so much for your answer!

I like thing to be in order ;)

I figured out I could get Exiftool to sort the keywords in one command if I would use a temporary user parameter as shown below.

exiftool -sep ', ' -userParam temp='Japan, Andorra, Island' '-Keywords<${temp;$_=join ", ",(sort{lc($a) cmp lc($b)} split ", ")}' FileOrDir

I wonder if similar approach could be used to add new keywords and have Exiftool to join them together with existing keywords and also alphabetically order the keywords in one command. I am not familiar with Perl at all so it makes this slightly difficult... Thank you again so much for your help!

StarGeek

Quote from: marcobarrios on August 12, 2018, 05:53:23 PM
I wonder if similar approach could be used to add new keywords and have Exiftool to join them together with existing keywords and also alphabetically order the keywords in one command.

Yeah, it's possible.  You can use the undocumented feature $self->GetValue('Keywords') (see this example) but then you have to worry about duplicates.  At this point, I'd probably start thinking about making a user defined tag, but here's a working example, using the -p option instead of actually copying
C:\>exiftool -g1 -a -s -iptc:keywords y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : A, B, Birthday, C, D, ape, crocodile, dog, donkey

C:\>exiftool -userparam temp="xyzzy, plugh, crocodile, Birthday, King Kong"  -p "${temp;$_=join ', ',(sort{lc($a) cmp lc($b)} (split(', '),$self->GetValue('Keywords')));NoDups}" y:\!temp\Test3.jpg
A, ape, B, Birthday, C, crocodile, D, dog, donkey, King Kong, plugh, xyzzy


Edit: Forgot to mention, -sep ', ' would definitely be required for copying the new data into Keywords.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Quote from: StarGeek on August 12, 2018, 06:52:23 PM
You can use the undocumented feature $self->GetValue('Keywords')

This function is documented here.

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