MacOS – Copy spotlight comments into a text file

automatormacosphotosspotlight

Background: My parents over the years have been scanning in old photos as jpeg files. As they have been doing that they have been making notes on who/what is in the photo in the comments section of the file properties — The spotlight comments field. (Select a file and do a get info or pres Command-I). Unbeknownst to them, this info doesn't transfer when you move the file to a thumb-stick and put it on a Windows machine OR if you back it up to something like Google Drive/Drop box etc. Over the years they have amassed possibly hundreds (possibly thousands) of photos with comments… I really wish I would have know they are doing this and stopped them. Anyway my thought is a better way of doing this would be to have a txt file with the same name as the jpeg file and put the comments in there…

So, question, does anyone know if there is a program that will help me bulk export the comments to a txt file? What about Automator? Can that automate the process of taking a bunch of jpeg files, reading the comments section of the file properties on said files, then creating individual txt files with the same name as the photo and pasting those comments into the txt file? Any tips on how to actually do this would be appreciated. I've toyed with Automator and I cant seem to figure out how to do it.

I'm dying, otherwise it's going to take many many hours… possibly weeks/months to manually correct this.

Best Answer

You can use mdls to list the metadata attributes for a specific file. Here's how to show the comment for an imaginary file called myPhoto.jpeg in the Pictures folder:

mdls -name kMDItemFinderComment -r ~/Pictures/myPhoto.jpeg

And here's how to find all files on the computer that have a comment:

mdfind "kMDItemFinderComment=*"

Here's how to dump all comments for all files:

mdfind "kMDItemFinderComment=*" | xargs mdls -name kMDItemFinderComment -r

And dump the names of all commented files, along with their comments, as a CSV:

mdfind "kMDItemFinderComment=*" | xargs mdls -name kMDItemFinderComment -name kMDItemFSName -r | xargs -0 -n1 echo | awk 'NR%2==1{printf "\"" $0 "\","} NR%2==0{print "\"" $0 "\""}'