Skip to content

Instantly share code, notes, and snippets.

@dunhamsteve
Last active October 20, 2019 03:53
Show Gist options
  • Save dunhamsteve/2889617 to your computer and use it in GitHub Desktop.
Save dunhamsteve/2889617 to your computer and use it in GitHub Desktop.
Code to get and set kMDItemWhereFroms in python
# 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