Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I add a kMDItemWhereFroms attribute to a file?

I inspected the attribute with a third-party application and now I want to write a URL string as metadata from Terminal.


I tried plutil, xxd and xattr but still couldn't figure out the right form to write the bplist.


Any ideas?

MacBook Air, OS X Mountain Lion (10.8.5)

Posted on Nov 3, 2013 1:05 PM

Reply
31 replies

Nov 3, 2013 3:43 PM in response to iamsudo

You can place your links into an array in a regular XML property list and just write that, for example:

xattr -w 'com.apple.metadata:kMDItemWhereFroms' '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><string>http://www.where.do.I.come.from</string></array></plist>' /path/to/your/file


You can also use plutil to convert the XML to a binary property list by using binary1 for the convert format. When converting to the binary property list, you can use xxd to get the hexadecimal, but you also need to use the -x option with xattr so that it writes the hex and not the string.

Nov 3, 2013 3:51 PM in response to MrHoffman

MrHoffman wrote:


Usually something like...


xattr -w com.apple.metadata:kMDItemWhereFroms {url} {filename}


would attach the specified {url} onto the specified {filename}.


That writes the URL as a string, not as a binary plist. Try the following to see what I mean.


xattr -p com.apple.metadata:kMDItemWhereFroms sample.pdf | xxd -r -p | plutil -convert xml1 -o - -

Nov 4, 2013 4:51 AM in response to red_menace

red_menace wrote:


The documentation isn't too bad, at least on the options used here. I don't have a shell script, but have a working Applescript, with conversions back and forth.


I still couldn't manage to echo myURL | plutil | xxd | xattr. Actually, I cannot even pipe the hex value to xattr, and piping to a variable doesn't seem to work either.


I'd wrap the above in AppleScript, but since you have a working script, would you mind sharing it?

Nov 4, 2013 6:31 AM in response to iamsudo

I think with the argument arrangements you will probably have to use a variable (or file) for the intermediate values (that is what I did, anyway). The following script doesn't check if the current setting is not a property list (which wouldn't show up in the Finder or Spotlight anyway), but has shell script pieces to get, set, and convert from/to binary:


-- view and manipulate 'com.apple.metadata:kMDItemWhereFroms' extended attribute # switches to alter script operation: property remove : false -- remove the attribute? property binary : true -- set binary plist? set action to "Set" if remove then set action to "Remove" set theFile to quoted form of POSIX path of (choose file with prompt action & " 'com.apple.metadata:kMDItemWhereFroms' attribute:") set attribute to missing value try -- get existing attribute as property list   set attribute to (do shell script "xattr -p com.apple.metadata:kMDItemWhereFroms " & theFile & "  | xxd -r -p | plutil -convert xml1 -o - -") -- convert from binary on error -- oops, not a binary plist, so try XML   try -- skip error if no attribute     set attribute to (do shell script "xattr -p com.apple.metadata:kMDItemWhereFroms " & theFile)   end try end try log attribute if remove then -- delete attribute   try -- skip error if no attribute     do shell script "xattr -d com.apple.metadata:kMDItemWhereFroms " & theFile   end try else -- add   if attribute is missing value then -- create example property list     set plist to "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version=\"1.0\"> <array>     <string>http://www.where.do.I.come.from</string>     <string>http://www.some.other.place</string> </array> </plist>"   else -- reuse existing     set plist to attribute   end if   if binary then -- convert and set binary plist     set bplist to do shell script "echo " & quoted form of plist & " | plutil -convert binary1 -o - - | xxd -p"     do shell script "xattr -w -x com.apple.metadata:kMDItemWhereFroms " & bplist & space & theFile   else -- set XML plist     do shell script "xattr -w com.apple.metadata:kMDItemWhereFroms " & quoted form of plist & space & theFile   end if end if

Nov 13, 2013 8:57 AM in response to red_menace

Since in my workflow I get input from another application to set theURLs and theFilesQuoted I used the Property List Suite which seems to escape all special characters so far.


tell application "System Events"

set PlistFile to "~/Desktop/kMDItemWhereFroms_tmp.plist"

set PlistFirstItem to makenewproperty list itemwith properties {kind:list}

set PlistXML to makenewproperty list filewith properties {name:PlistFile, contents:PlistFirstItem}


makenewproperty list itemat end of every property list item of contents of PlistXMLwith properties {kind:string, value:theURLs}

set PlistBinary to do shell script "plutil -convert binary1 " & PlistFile & " -o - | xxd -p"

do shell script "xattr -w -x com.apple.metadata:kMDItemWhereFroms " & PlistBinary & space & theFilesQuoted

do shell script "mv " & PlistFile & " ~/.Trash/"

end tell

Dec 5, 2013 11:55 AM in response to iamsudo

I like this , but you have a small typo in that you need comma-separated quoted strings, not quoted, comma-separated strings in the input to plutil.


myUrl=`echo \(\'http://www.google.com\',\'https://www.google.com\'\) | plutil -convert binary1 -o - - | xxd -p -c 16 -u` ; xattr -xw com.apple.metadata:kMDItemWhereFroms "$myUrl" test.txt


Or formatted for inclusion in a shell script:


url1="http://www.example.com/test.txt" ; \

url2="http://www.example.com/?search=test.txt" ; \

xattr -xw com.apple.metadata:kMDItemWhereFroms \

"`echo \(\'$url1\',\'$url2\'\) | \

plutil -convert binary1 -o - - | \

xxd -p -c 16 -u`" \

test.txt

How can I add a kMDItemWhereFroms attribute to a file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.