4

Is it possible to copy an individual file and preserve its comment?

Here is what I have tried. Nothing here works for me on macOS 14.3.1 and Zsh 5.9 (x86_64-apple-darwin23.0).

cp file.txt file-copy.txt
cp -a file.txt file-copy.txt
rsync file.txt file-copy.txt
rsync -E file.txt file-copy.txt

The tags (red, orange, etc.) are always preserved, but not the comment.

If you run xattr file-copy.txt, you will see that the com.apple.metadata:kMDItemFinderComment extended attribute itself is preserved, but if you run mdls -name kMDItemFinderComment file-copy.txt you will see that its value is "".

2
  • 2
    This is a bit of a side-step but applescript can duplicate a file and the result will have whatever finder comment that the original did. An example command would be tell application "Finder" to duplicate file "file.txt" of folder folderPath, where folderPath would be the path to the file's containing folder. It is possible to call applescript from the shell using the osascript command. It's a bit quirky but depending upon your specific use case, it might provide a solution. Duplicate has several options which can be found in its applescript dictionary, and there is man osascript.
    – Mockman
    Commented Mar 15 at 16:54
  • 1
    Wow, thanks a lot!
    – user480875
    Commented Mar 15 at 18:27

2 Answers 2

4

Finder Comments are a mess and unreliable. The best source for this is Howard Oakley's Would you like to Comment or Finder Comment?

Originally Finder Comments were stored in the folder in the .DS_Store file. Since macOS (I don't know which version) they are often stored in the file's extended attribute com.apple.metadata:kMDItemFinderComment. But can continue to be in .DS_Store as well or instead.

The extended attribute belongs to the file and is kept with the file when moving or copying. The Finder Comment in .DS_Store belongs to a conjunction of folder and file; that is broken when you move the file and needs to created afresh in the .DS_Store of the new folder.

Copying (and duplicating) is hopelessly unreliable for Finder Comments - see Howard Oakley's post.

Note that Spotlight may use Finder Comments stored in either .DS_Store or as an extended attribute. Both sources are used to populate mdls's kMDItemFinderComment. As a result mdls is not a reliable indicator of what is going on.

A better solution is to use file Comments which are stored as the kMDItemComment extended attribute (and move with the file) and are sometimes displayed by Finder in Get Info's More Info section. But more reliably shown using xattr or mdls and, of course, are available to Spotlight searches,

You say in your final paragraph: If you run xattr file-copy.txt, you will see that the com.apple.metadata:kMDItemFinderComment extended attribute itself is preserved, but if you run mdls -name kMDItemFinderComment file-copy.txt you will see that its value is "".

What I think has happened is that the extended attribute has been copied with the file, but a new blank Finder Comment has been created in .DS_Store. And then Spotlight has used the blank one in .DS_Store - and this is what mdls shows. Remember that mdls reports metadata in the spotlight index and not directly the content of extended attributes or .DS_Store. It can, for Finder Comments, be populated from either .DS_Store or from the file's extended attribute.

In conclusion: don't use Finder Comments.

0
2

Use ditto with no arguments instead of cp to get metadata copied for command line file duplication tasks.

There appears to be a finder bug where a file with legitimate kMDItemFinderComment metadata is not shows in Finder. I have yet to isolate why / how / when that happens or if it's truly a bug on some versions of macOS including 14.4.

12
  • Many thanks. Indeed, mdls -name kMDItemFinderComment file-copy.txt returns the comment, it works. But the comment still missing in the Finder, in the Get Info window. (killall Finder doesn't seem to solve this.) Do you see it there?
    – user480875
    Commented Mar 14 at 13:29
  • I do not see the comment in finder. Strange days! I wonder what’s up here…
    – bmike
    Commented Mar 14 at 13:33
  • cp -p appears to have the same effect as ditto - including comments showing up in mdls but not in Finder.
    – Gairfowl
    Commented Mar 14 at 13:34
  • 1
    Even failing to show this after shutting down the OS @benwiggy in my book, it’s a true bug if the OS can’t render data that was recorded to disk before the OS started up and the test happens.
    – bmike
    Commented Mar 14 at 15:19
  • 1
    Not, I suggest, a bug in Finder. Rather that spotlight indexing is indeterminate as to whether the xattr or .DS_Store is used as source of mdls/spotlight Finder Comment.
    – Gilby
    Commented Mar 15 at 6:27

You must log in to answer this question.