ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: camner on October 12, 2024, 10:41:12 PM

Title: Add new line character & text to existing tag
Post by: camner on October 12, 2024, 10:41:12 PM
I'd like to take the contents of the XMP-DC tag and add a new line character and additional text, but I can't figure out how to do this.  I tried:
exiftool '-XMP-DC:Title < ${XMP-DC:Title} \n[world]' and exiftool '-XMP-DC:Title < ${XMP-DC:Title} \\n[world]' (in case I needed to escape the \n), but in both cases ExifTool is reading the \n as literal text. 

Per FAQ 21b I also tried:
exiftool -ec '-XMP-DC:Title < ${XMP-DC:Title} \n[world]'That didn't work, either.  But I'm sure there's a way to do this (I'm on a Mac, which is why I'm using single rather than double quotes).
Title: Re: Add new line character & text to existing tag
Post by: StarGeek on October 12, 2024, 10:44:51 PM
FAQ #21, How do I read/write values containing newline characters? (https://exiftool.org/faq.html#Q21)
Title: Re: Add new line character & text to existing tag
Post by: camner on October 12, 2024, 11:16:51 PM
I did mention in my original post that I did try the -ec method outlined in FAQ 21b.

I just tried exiftool -E '-XMP-DC:Title < ${XMP-DC:Title}&#xa added text'This also failed (the &#xa was taken as literal text and appended literally to the Title field)

This feels as if I am missing something rather small.
Title: Re: Add new line character & text to existing tag
Post by: StarGeek on October 13, 2024, 12:07:50 AM
You dropped the semicolon off of the HTML entity.

What version of exiftool are you using?
exiftool -ver

The -ec (-escapeC) option (https://exiftool.org/exiftool_pod.html#E--ex--ec--escapeHTML--escapeXML--escapeC) was added in version 11.54, July 2, 2019, but wasn't documented until version 11.91 (https://exiftool.org/ancient_history.html#v11.91).

Example. Here, the -E option is used with the new line HTML entity, &#xa;. The -b (-binary) option (https://exiftool.org/exiftool_pod.html#b---b--binary---binary) is used to show the exact contents of the Title and the -j (-json) option (https://exiftool.org/exiftool_pod.html#j-JSONFILE--json) uses the same \n to embed new lines and indicates its location.
C:\>exiftool -G1 -a -s -Title y:\!temp\Test4.jpg
[XMP-dc]        Title                           : Title

C:\>exiftool -E "-XMP-DC:Title<${XMP-DC:Title}&#xa;added text" y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -Title -b y:\!temp\Test4.jpg
Title
added text
C:\>exiftool -G1 -a -s -Title -j y:\!temp\Test4.jpg
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Title": "Title\nadded text"
}]

This example uses the command from the FAQ with the -ec option, though I changed the tag to Description
C:\>exiftool -P -overwrite_original -ec "-Description=line 1\nline 2" y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -Description y:\!temp\Test4.jpg
[XMP-dc]        Description                     : line 1.line 2

C:\>exiftool -G1 -a -s -b -Description y:\!temp\Test4.jpg
line 1
line 2
C:\>exiftool -G1 -a -s -j -Description y:\!temp\Test4.jpg
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Description": "line 1\nline 2"
}]

I have double quotes in my examples because I'm using Windows CMD. If you are using Mac/Linux, change the double quotes into single quotes. Windows Powershell might work with single quotes (but not double quotes), but if it doesn't, switch to CMD.
Title: Re: Add new line character & text to existing tag
Post by: camner on October 13, 2024, 08:13:42 PM
Quote from: StarGeek on October 13, 2024, 12:07:50 AMYou dropped the semicolon off of the HTML entity.
Drat!  I hate it when I can't figure out easy stuff like this! Thanks.

QuoteWhat version of exiftool are you using?
exiftool -ver
12.97

QuoteThe -ec (-escapeC) option (https://exiftool.org/exiftool_pod.html#E--ex--ec--escapeHTML--escapeXML--escapeC) was added in version 11.54, July 2, 2019, but wasn't documented until version 11.91 (https://exiftool.org/ancient_history.html#v11.91).

Example. Here, the -E option is used with the new line HTML entity, &#xa;. The -b (-binary) option (https://exiftool.org/exiftool_pod.html#b---b--binary---binary) is used to show the exact contents of the Title and the -j (-json) option (https://exiftool.org/exiftool_pod.html#j-JSONFILE--json) uses the same \n to embed new lines and indicates its location.
C:\>exiftool -G1 -a -s -Title y:\!temp\Test4.jpg
[XMP-dc]        Title                          : Title

C:\>exiftool -E "-XMP-DC:Title<${XMP-DC:Title}&#xa;added text" y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -Title -b y:\!temp\Test4.jpg
Title
added text
When I redid this with the darn semicolon in place, it worked as it did for you.
QuoteC:\>exiftool -G1 -a -s -Title -j y:\!temp\Test4.jpg
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Title": "Title\nadded text"
}]
I'm not following what this part in the square brackets & braces is doing

QuoteThis example uses the command from the FAQ with the -ec option, though I changed the tag to Description
C:\>exiftool -P -overwrite_original -ec "-Description=line 1\nline 2" y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -Description y:\!temp\Test4.jpg
[XMP-dc]        Description                    : line 1.line 2

C:\>exiftool -G1 -a -s -b -Description y:\!temp\Test4.jpg
line 1
line 2
C:\>exiftool -G1 -a -s -j -Description y:\!temp\Test4.jpg
[{
  "SourceFile": "y:/!temp/Test4.jpg",
  "XMP-dc:Description": "line 1\nline 2"
}]
This work for me as well. I don't understand why what I did in my original post with the \n didn't work.

Thanks for your help with this!  
Title: Re: Add new line character & text to existing tag
Post by: StarGeek on October 13, 2024, 08:49:43 PM
Quote from: camner on October 13, 2024, 08:13:42 PMI'm not following what this part in the square brackets & braces is doing

That is the result of the -j (-json) option (https://exiftool.org/exiftool_pod.html#j-JSONFILE--json), which outputs the data in JSON format. I used that as it would explicitly show where the \n was located. I could also have used the -php option (https://exiftool.org/exiftool_pod.html#php) which would be similar, but formatted for php.
Title: Re: Add new line character & text to existing tag
Post by: camner on October 14, 2024, 01:38:08 PM
Now I am applying what I have learned here to what Lightroom calls the "Caption," and this technique is resulting in behavior I don't understand.

When Lightroom writes out metadata to the image files, three tags are written: IPTC:Caption-Abstract, IFD0:ImageDescription, and XMP-dc:Description.

(I am doing this all in ExifTool and not involving Lightroom at all other than to make sure that the Image file is exactly as it would be written by Lightroom)

Here's my first example:
Last login: Mon Oct 14 10:16:54 on ttys000
rc@iMac ~ % exiftool -G1 -a -s -IPTC:Caption-Abstract -IFD0:ImageDescription -XMP-dc:Description /rc/temp.jpg
[IPTC]          Caption-Abstract                : Caption
[IFD0]          ImageDescription                : Caption
[XMP-dc]        Description                     : Caption
rc@iMac ~ % exiftool -E '-IPTC:Caption-Abstract  =[Correct month & yr]' \
 '-IPTC:Caption-Abstract  < ${IPTC:Caption-Abstract }&#xa;[Correct month & yr]' \
'-IFD0:ImageDescription =[Correct month & yr]' \
'-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' \
'-XMP-dc:Description =[Correct month & yr]' \
'-XMP-dc:Description < ${XMP-dc:Description}&#xa;[Correct month & yr]' \
'-IPTCDigest=new' /rc/temp.jpg

    1 image files updated
rc@iMac ~ % exiftool -G1 -a -s -IPTC:Caption-Abstract -IFD0:ImageDescription -XMP-dc:Description /rc/temp.jpg
[IPTC]          Caption-Abstract                : Caption.[Correct month & yr]
[IFD0]          ImageDescription                : [Correct month & yr]
[XMP-dc]        Description                     : [Correct month & yr]
As you can see, the three tags start out identically, I run my command, but only IPTC:Caption-Abstract correctly appends the text I want to add.

Now look at this:
rc@iMac ~ % exiftool -G1 -a -s -IPTC:Caption-Abstract -IFD0:ImageDescription -XMP-dc:Description /rc/temp.jpg
[IPTC]          Caption-Abstract                : Caption
[IFD0]          ImageDescription                : Caption
[XMP-dc]        Description                     : Caption
rc@iMac ~ % exiftool -E '-IFD0:ImageDescription =[Correct month & yr]' \
'-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' \
'-XMP-dc:Description =[Correct month & yr]' \
'-XMP-dc:Description < ${XMP-dc:Description}&#xa;[Correct month & yr]' \
'-IPTC:Caption-Abstract  =[Correct month & yr]' \
 '-IPTC:Caption-Abstract  < ${IPTC:Caption-Abstract }&#xa;[Correct month & yr]' \
'-IPTCDigest=new' /rc/temp.jpg
    1 image files updated
rc@iMac ~ % exiftool -G1 -a -s -IPTC:Caption-Abstract -IFD0:ImageDescription -XMP-dc:Description /rc/temp.jpg
[IPTC]          Caption-Abstract                : [Correct month & yr]
[IFD0]          ImageDescription                : Caption.[Correct month & yr]
[XMP-dc]        Description                     : [Correct month & yr]

This time only IFD0:ImageDescription correctly has the desired text appended!

I tested with putting the remaining tag first, and the behavior was similar. Only the first tag in the command works correctly in terms of getting the desired text appended.

Is there something about the way ExifTool works that explains this behavior? (If so, is there a workaround?)
Title: Re: Add new line character & text to existing tag
Post by: Phil Harvey on October 15, 2024, 07:49:52 AM
FAQ 22 (https://exiftool.org/faq.html#Q22) explains this.  Specifically, see point #3.

- Phil
Title: Re: Add new line character & text to existing tag
Post by: StarGeek on October 15, 2024, 09:43:42 AM
The result is that your command because this

exiftool -E '-IFD0:ImageDescription =[Correct month & yr]' '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' '-XMP-dc:Description < ${XMP-dc:Description}&#xa;[Correct month & yr]' '-IPTC:Caption-Abstract  < ${IPTC:Caption-Abstract }&#xa;[Correct month & yr]' '-XMP-dc:Description =[Correct month & yr]' '-IPTC:Caption-Abstract  =[Correct month & yr]'  '-IPTCDigest=new' /rc/temp.jpg

All the -TagsFromFile commands are assigned after the ImageDescription assignment, then are overridden by the subsequent assignments.

This is probably something that would have stumped me, but I probably encountered it previously because I have a habit of setting assignments earlier in the command.
Title: Re: Add new line character & text to existing tag
Post by: camner on October 15, 2024, 02:25:46 PM
Quote from: Phil Harvey on October 15, 2024, 07:49:52 AMFAQ 22 (https://exiftool.org/faq.html#Q22) explains this.  Specifically, see point #3.
Thanks.  I had read FAQ 22 before constructing the command, but I don't think I fully understood the implications.  Let me see if I have this right:
exiftool -E '-IFD0:ImageDescription =[Correct month & yr]' '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]'With this command alone, only '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' is executed because the original command has two assignments of IFD0:ImageDescription and per ExifTool behavior, when a given tag is assigned more than once in a single ExifTool command, the rightmost assignment takes precedence.

Do I have this right? If I'm right about this, then there was no point in my constructing the command this way, because only the second assignment (the one that appends) of IFD0:ImageDescription would ever take effect. Yes?


Quote from: StarGeek on October 15, 2024, 09:43:42 AMThe result is that your command because this

exiftool -E '-IFD0:ImageDescription =[Correct month & yr]' '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' '-XMP-dc:Description < ${XMP-dc:Description}&#xa;[Correct month & yr]' '-IPTC:Caption-Abstract  < ${IPTC:Caption-Abstract }&#xa;[Correct month & yr]' '-XMP-dc:Description =[Correct month & yr]' '-IPTC:Caption-Abstract  =[Correct month & yr]'  '-IPTCDigest=new' /rc/temp.jpg

All the -TagsFromFile commands are assigned after the ImageDescription assignment, then are overridden by the subsequent assignments.

This is probably something that would have stumped me, but I probably encountered it previously because I have a habit of setting assignments earlier in the command.
Wow!  I never would have sussed this out!  If I understand THIS correctly, ALL THREE of the implicit -TagsFromFile commands are processed together, when the 1st one is encountered.  BUT, since there are later assignments for the 2nd and 3rd tags, those later assignments override the 2nd and 3rd -TagsFromFile commands.
Yes?
Title: Re: Add new line character & text to existing tag
Post by: Phil Harvey on October 15, 2024, 03:59:52 PM
Quote from: camner on October 15, 2024, 02:25:46 PMexiftool -E '-IFD0:ImageDescription =[Correct month & yr]' '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]'With this command alone, only '-IFD0:ImageDescription < ${IFD0:ImageDescription}&#xa;[Correct month & yr]' is executed

No.  The assignment (=) is executed always, and the copy (<) will only override the earlier assignment if IFD0:ImageDescription already exists in the file.

QuoteALL THREE of the implicit -TagsFromFile commands are processed together, when the 1st one is encountered.  BUT, since there are later assignments for the 2nd and 3rd tags, those later assignments override the 2nd and 3rd -TagsFromFile commands.
Yes?

Basically, yes.

For example, this command:

exifool -TAG1=x "-TAG1<TAG2" -TAG3=y "-TAG3<TAG4" ...

implies the -tagsFromFile option, in this position:

exifool -TAG1=x -tagsfromfile @ "-TAG1<TAG2" -TAG3=y "-TAG3<TAG4" ...

but all copied tags are done together immediately after the -tagsFromFile, so the order is effectively like this:

exifool -TAG1=x -tagsfromfile @ "-TAG1<TAG2" "-TAG3<TAG4" -TAG3=y ...

and the "-TAG3<TAG4" argument is always overridden by -TAG3=y

- Phil