Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Manually set extended attributes on arbitrary files System
Files downloaded using Safari have their source URL recorded within the file's metadata in the form of the com.apple.metadata:kMDItemWhereFroms extended attribute. In particular, this is convenient because the information is available in the Finder (using 'Get Info') or can be used as the basis of Spotlight searches.

Unfortunately, the feature is not widely supported among other browsers and not at all for files received directly from other people.

While not as convenient as having it available as a built-in function, users of other browsers can still enjoy the organizational benefits of this feature by manually writing 'where from' information for files of their choosing.

The 'where from' metadata can be viewed on the command line using /usr/bin/xattr like this:
xattr -p com.apple.metadata:kMDItemWhereFroms path_to_file
However, the format is not immediately obvious, apart from the hint that it is bplist (binary plist) data.

It turns out, as intuitively deduced by a poster in the below bugzilla link, the same data represented as xml is an acceptable format for 'kMDItemWhereFroms', and is much easier for the user to work with.

In short, simply substitute the desired URL (or any arbitrary unicode text for that matter) between the tags and then supply the path to the file for the following command:
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.from.where.do.I.com</string></array></plist>' path_to_file
The effect should be immediate, and it should be possible to view your modification by opening a new 'Get Info' window for the target file.

Full credit for this hint goes to this bugzilla thread, where this was used for Firefox.

[crarko adds: I tested this, and it works as described.]
    •    
  • Currently 3.40 / 5
  You rated: 5 / 5 (5 votes cast)
 
[11,112 views]  

Manually set extended attributes on arbitrary files | 3 comments | Create New Account
Click here to return to the 'Manually set extended attributes on arbitrary files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Manually set extended attributes on arbitrary files
Authored by: mkhaw on Jan 04, '11 09:07:27AM
I wrote a shell script to make it a bit easier. I call it 'wherefrom_into_fileinfo' and tested it by writing this hint's URL into its metadata.


#!/bin/sh

# write the url from which a file came into its metadata
# see http://hints.macworld.com/article.php?story=20110102222441243

# command line flags sanity check
# $1 is target pathname
# $2 is source url to write into target pathname's metadata
case $# in
  2)	;;
  *)	echo "Usage: $0 pathname source_url"
	exit 1;;
esac

# verify existence of source url
# sed script extracts HTTP status code returned by curl document info request
http_status=`curl -I "$2" 2> /dev/null | sed -n -e '/^HTTP/s/^[^ ]* \([0-9]*\) .*$/\1/p'`
case $http_status in
  2??)	;;
  *)	echo "URL '$2' not found"
	exit 1;;
esac

# fail if target file isn't writeable or doesn't exist
test -w $1 || { echo "Can't write to $1"; exit 1; }

# write metadata
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>'"$2"'</string></array></plist>' "$1"


[ Reply to This | # ]
Re: Manually set extended attributes on arbitrary files
Authored by: Uncle Asad on Jan 04, '11 02:00:36PM

Thanks for discovering the plist structure and that xml1 would work! I had tried for some time to guess and never gotten it right (and I had long since forgotten about that Gecko bug because Camino has WhereFroms support built-in already).

Also, if you want to include the referrer URL (like Camino and Safari do), too, just add a second <string> inside the <array> to hold that URL.



[ Reply to This | # ]
Manually set extended attributes on arbitrary files
Authored by: scottkitts on Jan 04, '11 02:53:03PM

xattr is only available in 10.5 and later, not for all versions of OS X as the hint label implies.



[ Reply to This | # ]