osxmetadata: Setting `kMDItemWhereFroms` doesn't show in Finder
This doesn’t cause the URL to show in Finder:
import plistlib
from osxmetadata import *
url = "https://apple.com"
out_path = "/tmp/test_md.txt"
with open(out_path, "w") as f:
f.write("hi")
meta = OSXMetaData(out_path)
meta.update_attribute(kMDItemWhereFroms, [url])
The attribute also is not listed with mdls:
$ mdls /tmp/test_md.txt
kMDItemFSContentChangeDate = 2022-09-20 14:56:00 +0000
kMDItemFSCreationDate = 2022-09-20 14:56:00 +0000
kMDItemFSCreatorCode = ""
kMDItemFSFinderFlags = 0
kMDItemFSHasCustomIcon = 0
kMDItemFSInvisible = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSIsStationery = 0
kMDItemFSLabel = 0
kMDItemFSName = "testmd.txt"
kMDItemFSNodeCount = 2
kMDItemFSOwnerGroupID = 0
kMDItemFSOwnerUserID = 501
kMDItemFSSize = 2
kMDItemFSTypeCode = ""
But it IS there, and looks to be correctly formatted as a binary plist:
$ xattr -l /tmp/test_md.txt
com.apple.metadata:kMDItemWhereFroms: bplist00�_https://apple.com
I’m on Monterey 12.6 (21G115)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 21 (13 by maintainers)
Commits related to this issue
- Update README_DEV.md per #61 — committed to RhetTbull/osxmetadata by RhetTbull 2 years ago
- Update README.md per #61 — committed to RhetTbull/osxmetadata by RhetTbull 2 years ago
I’ll add a note to the docs about temporary files. I got the test suite running last night in GitHub actions (via a BigSur VM, the latest available in GitHub). Interestingly the same three tests fail with the same result. Something about
kMDItemAuthorsisn’t right for macOS > Catalina. I’ll open a separate issue for this.Whoa, what a bizarre quirk! Indeed, when I write something to my home directory instead, the Where froms are set as expected and are shown immediately in Finder. Still doesn’t explain the test errors…
But maybe this can be closed after all? Probably a good idea to document this
/tmpquirk as well. Thanks so much!I’m on Monterey 12.6. Hopefully it’s not an OS version issue… but I’ll give the tests a run and report back.
@nk9 would you mind cloning the repo then running the test suite? See instructions in README_DEV.md for how to install/build the package.
@nk9 I’ve release version 1.0.0 of osxmetadata that fixes this bug and several others. It’s a complete rewrite to use the native macOS calls to get/set metadata. It does change the API in breaking ways though so check out the README.md.
This is incredible, thank you! I was thinking that I should try using
MDItemSetAttributeas used in the browser code above, but hadn’t gotten around to it since I thought it would have to be in ObjC or Swift. Kudos for working it out, and so quickly!@nk9 I’ve figured out how to call the undocumented MDItemSetAttribute from python. The following snippet (also as a gist) will set kMDItemWhereFroms if called like this:
python setmd.py file.txt kMDItemWhereFroms array google.comIt doesn’t yet handle types other than string or array (need to reference here for full list of attributes/types) – kMDItemWhereFroms is an array. Finder comments and Finder tags cannot be set this way. Finder comments must be set by AppleScript and Finder tags by xattr using
com.apple.metadata:_kMDItemUserTags.I verified that both mdls and Finder show the updated kMDItemWhereFroms when set this way.
More to come – will look at adapting this for osxmetadata.