Last active
October 20, 2019 03:53
-
-
Save dunhamsteve/2889617 to your computer and use it in GitHub Desktop.
Code to get and set kMDItemWhereFroms in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code is public domain, share and enjoy. | |
from Foundation import NSPropertyListSerialization | |
from xattr import setxattr, getxattr | |
kMDItemWhereFroms = "com.apple.metadata:kMDItemWhereFroms" | |
def set(path, *data): | |
"""Set kMDItemWhereFroms on a file. Pass in an array of strings.""" | |
plist, err = NSPropertyListSerialization.dataWithPropertyList_format_options_error_(data, 200, 0, None) | |
if err or not plist: | |
return | |
setxattr(path, kMDItemWhereFroms, plist) | |
def get(path): | |
"""Get kMDItemWhereFroms from a file, returns an array of strings or None if no value is set.""" | |
try: | |
plist = buffer(getxattr(path, kMDItemWhereFroms)) | |
if plist: | |
data = NSPropertyListSerialization.propertyListWithData_options_format_error_(plist, 0, None, None)[0] | |
return data | |
except KeyError: | |
pass | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment