ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: ejm554 on February 07, 2019, 01:09:39 PM

Title: Using -if with macOS MDItemUserTags
Post by: ejm554 on February 07, 2019, 01:09:39 PM
Hello. I have several XMP files that I'm trying to process in a directory. I'd like to only process the ones that contain a specific macOS Finder tag (MDItemUserTags).

Although I'd like to eventually move these files with ExifTool, I'd first like to return a simple list.

For sake of example, here is a list of four XMP files. The first has the tag I want ("Import_2019-01-15") in the middle of the tag list; files 2 & 3 also have the tag I want, but contain a trailing ".0". (This represents a tag named 'zero' that got attached the file unintentionally. It's preceded by a line-break character, which ExifTool swaps out with a period as per the documentation.) The final file has no tags.

$ exiftool -FileName -XAttrMDItemUserTags -ext XMP .
======== ./201901.IMG_1097.0.xmp
File Name                       : 201901.IMG_1097.0.xmp
X Attr MD Item User Tags        : Aa_Test, Import_2019-01-15, Zz_Test
======== ./201901.IMG_1213.0.xmp
File Name                       : 201901.IMG_1213.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_1214.0.xmp
File Name                       : 201901.IMG_1214.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_9999.0.xmp
File Name                       : 201901.IMG_9999.0.xmp
X Attr MD Item User Tags        :
    1 directories scanned
    4 image files read


How do I process files that contains these Finder tags?

Using -if with eq returns nothing:

$ exiftool -FileName -XAttrMDItemUserTags -if '$XAttrMDItemUserTags eq "Import_2019-01-15"' -ext XMP .
    1 directories scanned
    4 files failed condition
    0 image files read


Using -if with gt (greater than) returns only those files where the list starts with the string I want:

$ exiftool -FileName -XAttrMDItemUserTags -if '$XAttrMDItemUserTags gt "Import_2019-01-15"' -ext XMP .
======== ./201901.IMG_1213.0.xmp
File Name                       : 201901.IMG_1213.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_1214.0.xmp
File Name                       : 201901.IMG_1214.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
    1 directories scanned
    2 files failed condition
    2 image files read


Note that I also tried experimenting with some RexEx, but it returned duplicates. Perhaps that is not important, but when I tried to use it to move/copy the files into another directory option, it didn't behave as I expected:

$ exiftool -FileName -XAttrMDItemUserTags -if '$XAttrMDItemUserTags =~ /Import_2019-01-15/' * -ext XMP .
======== 201901.IMG_1097.0.xmp
File Name                       : 201901.IMG_1097.0.xmp
X Attr MD Item User Tags        : Aa_Test, Import_2019-01-15, Zz_Test
======== 201901.IMG_1213.0.xmp
File Name                       : 201901.IMG_1213.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== 201901.IMG_1214.0.xmp
File Name                       : 201901.IMG_1214.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_1097.0.xmp
File Name                       : 201901.IMG_1097.0.xmp
X Attr MD Item User Tags        : Aa_Test, Import_2019-01-15, Zz_Test
======== ./201901.IMG_1213.0.xmp
File Name                       : 201901.IMG_1213.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_1214.0.xmp
File Name                       : 201901.IMG_1214.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
    1 directories scanned
    6 image files read

$ exiftool -out -Directory<Foo -if '$XAttrMDItemUserTags =~ /Import_2019-01-15/' * -ext XMP .
    2 directories scanned
    6 files failed condition
    0 image files read
Title: Re: Using -if with macOS MDItemUserTags
Post by: StarGeek on February 07, 2019, 03:04:46 PM
Quote from: ejm554 on February 07, 2019, 01:09:39 PM
Using -if with eq returns nothing:

The reason for this is because eq is an exact match.  If it contains other items, it won't match exactly.

QuoteUsing -if with gt (greater than) returns only those files where the list starts with the string I want:

This won't work because it still compares the whole string.  "Aa_Test" is less than "Import".  If you had one that started "Zz_test" or even "aa_test" (lowercase letters are greater than upper case letters), those would also be selected.

QuoteNote that I also tried experimenting with some RexEx, but it returned duplicates. Perhaps that is not important, but when I tried to use it to move/copy the files into another directory option, it didn't behave as I expected:

So if I'm understanding you correctly, you want "Import_2019-01-15", but not "Import_2019-01-15(LF)#"?  Try this:
-if '$XAttrMDItemUserTags=~/Import_2019-01-15(,|$)/'

Here, the regex is looking for the string you want, followed by either a comma or the end of the string.  The dollar sign is a special character in RegEx which stands for end of the string.

Title: Re: Using -if with macOS MDItemUserTags
Post by: ejm554 on February 07, 2019, 03:51:24 PM
Quote from: StarGeek on February 07, 2019, 03:04:46 PM

QuoteSo if I'm understanding you correctly, you want "Import_2019-01-15", but not "Import_2019-01-15(LF)#"?

Actually, I wanted to return all files that contain the tag, "Import_2019-01-15". I modified your RegEx , leaving off the command and dollar sign. That did the trick. Thanks!

$ exiftool -FileName -XAttrMDItemUserTags -if '$XAttrMDItemUserTags=~/Import_2019-01-15/' -ext XMP .
======== ./201901.IMG_1097.0.xmp
File Name                       : 201901.IMG_1097.0.xmp
X Attr MD Item User Tags        : Aa_Test, Import_2019-01-15, Zz_Test
======== ./201901.IMG_1213.0.xmp
File Name                       : 201901.IMG_1213.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
======== ./201901.IMG_1214.0.xmp
File Name                       : 201901.IMG_1214.0.xmp
X Attr MD Item User Tags        : Import_2019-01-15.0
    1 directories scanned
    1 files failed condition
    3 image files read



Title: Re: Using -if with macOS MDItemUserTags
Post by: StarGeek on February 07, 2019, 04:11:49 PM
Quote from: ejm554 on February 07, 2019, 03:51:24 PM
Actually, I would like to return all files that contain the tag, "Import_2019-01-15". In my example, that would be three files. Your code returned just one file:

Ok, that's easier.  Two ways to do that, use whichever you find easier to understand:
Regex, but must escape special characters ()[]+{}.$|^ (not complete list), can add trailing i to be case insensitive
-if '$XAttrMDItemUserTags=~/Import_2019-01-15/'
Index (returns -1 if item doesn't appear), need to do more work to be case insensitive.
-if 'index($XAttrMDItemUserTags,"Import_2019-01-15")>=0'