ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: marcobarrios on August 12, 2018, 11:40:00 AM

Title: Alphabetically ordering list type tags?
Post by: marcobarrios on August 12, 2018, 11:40:00 AM
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!
Title: Re: Alphabetically ordering list type tags?
Post by: StarGeek on August 12, 2018, 01:12:43 PM
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
Title: Re: Alphabetically ordering list type tags?
Post by: marcobarrios on August 12, 2018, 05:53:23 PM
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!
Title: Re: Alphabetically ordering list type tags?
Post by: StarGeek on August 12, 2018, 06:52:23 PM
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 (https://exiftool.org/forum/index.php/topic,6811.msg34104.html#msg34104)) 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 (https://exiftool.org/exiftool_pod.html#p-FMTFILE-or-STR--printFormat) 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.
Title: Re: Alphabetically ordering list type tags?
Post by: Phil Harvey on August 13, 2018, 09:41:32 AM
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 (https://exiftool.org/ExifTool.html#GetValue).

- Phil