TransWikia.com

Using xattr to display macOS file comments

Stack Overflow Asked by Philip Kearns on August 26, 2020

If I use xattr on Python on macOS to display a file’s comments, it shows extraneous data:

>>> from xattr import xattr
>>> from pprint import pprint
>>> pprint(xattr('tmp.pk.new')[u'com.apple.metadata:kMDItemFinderComment'])
'bplist00_x10x0fExample commentx08x00x00x00x00x00x00x01x01x00x00x00x00x00x00x00x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x1a'
>>>

The comment is just ‘Example comment’, so what is the other data that’s being displayed?

One Answer

The finder comment is saved in a binary property list format, so you have to decode it to access the contents. In Python 3 you can just use the standard library module plistlib:

>>> from plistlib import loads
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> loads(contents)
'Example comment'

If you are still in Python 2, as your code suggests, you have to use an external library, as the buildin plistlib does not support the binary format, for example:

pip install biplist
python
>>> from biplist import readPlistFromString
>>> from xattr import xattr
>>> contents = xattr('tmp.pk.new')['com.apple.metadata:kMDItemFinderComment']
>>> readPlistFromString(contents)
'Example comment'

Correct answer by MrBean Bremen on August 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP