Search code examples
macosbashxattr

Getting a comment attribute from a file in Bash on osx


i'm trying to get the comment attribute of a file using xattr in bash on OSX. This is my code I'm running:

xattr -p com.apple.metadata:kMDItemFinderComment "/Users/fred/Dropbox/OSXStuff/app.icns"

However it is returning hexadecimal instead of the comment string. On the man page for xattr in osx it says that

if nils are detected in the data, the value is displayed in a hexadecimal representation

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xattr.1.html

What exactly is a "nil" and how did it get in the comment? Is it possible to get the string value of a file comment using xattr in OSX or are there always "nils" in the file comment?


Solution

  • Don't try to parse the extended attribute. Ask the metadata system (a.k.a. Spotlight) to directly obtain the information for you:

    mdls -raw -name kMDItemFinderComment "/Users/fred/Dropbox/OSXStuff/app.icns"
    

    The fact that the comment is kept in an extended attribute, let alone the format of that attribute, is an implementation detail that can't be relied upon.