1

Is there a way to batch edit MacOS comment metadata 'MDItemFinderComment'?

For example I have multiple files with comment like this:

file 1 apple NEW
file 2 orange NEW
file 3 grape NEW

I would like to batch remove the word 'NEW' from them

themys
  • 25
  • 4

1 Answers1

1

Using exiftool, you could use this command:

exiftool '-MDItemFinderComment<${MDItemFinderComment;s/ NEW$//}' /path/to/files/

This command applies regex to strip away (space)NEW from the end of the MDItemFinderComment in all files in the target directory. Add the -r (-recurse) option to recurse into subdirectories

StarGeek
  • 4,948
  • 2
  • 19
  • 30
  • That looks good, but I wonder if it can be tightened up a little with word boundaries before and after the word "NEW"? As it is, I think it might fail to do anything if the comment starts with "NEW" without a preceding space. And I think it might incorrectly change "SCIENTIST NEWTON" into "SCIENTIST TON". It is using Perl PCREs isn't it? – Mark Setchell Mar 28 '22 at 06:34
  • I *"think"* it *"might"* be better with `s/\wNEW\w//` – Mark Setchell Mar 28 '22 at 06:48
  • 1
    `\w` is word character, so that would require a `A-Za-z0-9_` before and after `NEW`. `\b` is word boundary. Assuming that `NEW` always appears a the end, adding the end of line marker would be better. Editing answer to add this. – StarGeek Mar 28 '22 at 14:31
  • Also trying to make sure there is not a trailing space, which probably doesn't matter overall, just something of an annoyance to me. And yes, exiftool is written in Perl, so it uses PCRE. – StarGeek Mar 28 '22 at 14:34
  • You already have my upvote. Thank you for thinking further about it and taking the time to clarify - appreciated. – Mark Setchell Mar 28 '22 at 14:59
  • Thank you @StarGeek for your help! It works perfectly! I have another question. If I need to delete a longer string of text from the comment box, maybe a string of url with this variables `/ - ? = & ~`, replacing 'NEW' with the url doesn't work, so what should it be? For example to delete `/crop/x0+0/optimize?Name=abc__&Num=123~789__&Key-Id=XYZ` from `image.jpg/crop/x0+0/optimize?Name=abc__&Num=123~789__&Key-Id=XYZ` – themys Mar 28 '22 at 17:59
  • 1
    The problem there is that a lot of those characters have special meanings in regular expressions (RegEx). Any of these characters requires a backslash to escape it `. ^ $ * + - ? ( ) [ ] { } \ | — /` (see [this StackOverflow answer and the one that follows it](https://stackoverflow.com/a/55760851/3525475)) So you would use `${MDItemFinderComment;s/\/crop\/x0\+0\/optimize\?Name\=abc__\&Num\=123\~789__\&Key\-Id\=XYZ$//}` instead. – StarGeek Mar 28 '22 at 18:31
  • 1
    Just realized that the equal signs were escaped in the above while they probably don't need to be. That's because I used the Perl `quotemeta()` function in a quick & dirty way to escape your string. `perl -e "print quotemeta('StringToEscape');"` It won't hurt anything to escape characters that don't need escaping. – StarGeek Mar 28 '22 at 18:33
  • Thank you so much for your help! I was missing a \ at the start when I manually added them in. `perl` helped me to make sure nothing is missing – themys Mar 28 '22 at 20:34