ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: StarGeek on May 19, 2018, 04:03:03 PM

Title: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: StarGeek on May 19, 2018, 04:03:03 PM
I made a tag so that when you write HierarchicalSubject, it will also write the leaf keyword to IPTC:Keywords and XMP:Subject.  So if you want to write something like "People|Family|John", it will write the full string to HierarchicalSubject and just "John" to IPTC:Keywords and XMP:Subject.  Setting -MyHS= should clear out all three tags.

It won't work with exiftool versions from 10.16 to 10.97, but should work with earlier or later versions.  Testing has been limited because of this, but it seems to work well.

MyHS => {
# Writes string to HierarchicalSubject and leaf keywords to IPTC:Keywords and XMP:Subject.
# Requires exiftool ver 10.13 or earlier, or 10.98 or later
# Ver 1.1
# Added temp variable to avoid random hash(?) based error
Require => 'XMP:HierarchicalSubject',
Writable => 1,
List => 'Bag',
WriteAlso => {
'XMP:HierarchicalSubject' =>'$val',
'IPTC:Keywords' => q{ return undef if not defined $val; my $x = $val; $x=~s/.*(?:^|\|)([^|]*)$/$1/; return $x;},
'XMP:Subject' => q{ return undef if not defined $val; my $x = $val; $x=~s/.*(?:^|\|)([^|]*)$/$1/; return $x;},
},
ValueConv => '$val',
},



Example Output:
C:\>exiftool_10.13.exe -myhs+="People|Family|John" -myhs+="People|Family|Jane" y:\!temp\Test3.jpg
    1 image files updated

C:\>exiftool -g1 -a -s -IPTC :Keywords -XMP:Subject -HierarchicalSubject y:\!temp\Test3.jpg
---- IPTC ----
Keywords                        : John, Jane
---- XMP-lr ----
HierarchicalSubject             : People|Family|John, People|Family|Jane
---- XMP-dc ----
Subject                         : John, Jane


Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: StarGeek on May 20, 2018, 03:12:43 AM
I have to say that this tag is working out better than I had hoped.  I was only thinking of writing tags when I started, but didn't realize that it could also remove individual tags with -MyHS-=data.  And setting it to nothing (-MyHS=) will clear all three tags.  This will make management of HierarchicalSubject and the individual keywords much easier.
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: Phil Harvey on May 20, 2018, 09:30:27 AM
Looks great.

I'll be rolling out ExifTool 10.98 in a few days.

- Phil
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: StarGeek on May 22, 2018, 10:00:12 PM
With more testing on 10.98, I was running into the occasional problem that only the leaf word was being added to HierarchicalSubject.  I suspect it was the fact that you can't depend upon the order of a hash and that I was changing the value of $val.  Now $val is assigned to a temp variable and the regex is performed on that and then returned. 

Original post has been updated.  Now it's time to re-write a whole lot of commands to use this.
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: Phil Harvey on May 23, 2018, 07:34:58 AM
I don't remember what your original code looked like, but I usually do sort keys %HASH to avoid the hash order problem.

- Phil
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: mnaoumov on June 11, 2021, 11:59:55 AM
I put it inside

Quote'Image::ExifTool::Composite' => {

I tried to use it but I get

QuoteWarning: Sorry, MyHS is not writable
Nothing to do.

it has
Writable => 1

I also tried to change to
Writable => 'string'

But it doesn't help

I am using ExifTool 12.11 on Windows

Please assist
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: mnaoumov on June 11, 2021, 12:16:54 PM
Nevermind, found it

I had to put it under

%Image::ExifTool::UserDefined::xxx = (

which was not obvious (to me) :)
Title: Re: Tag to write Keywords, Subject, and HierarchicalSubject
Post by: StarGeek on June 14, 2021, 09:36:50 PM
Quote from: mnaoumov on June 11, 2021, 12:16:54 PM
which was not obvious (to me) :)

Sorry, I'll have to make this easier to work with when I get a chance.  I'm trying to remember if my local copy has a fix that I may not have put into this version.  While I use this a lot, it didn't seem to have generated much interest, so I may not have kept it updated.