OS X: Find source URL of downloaded file

related: shell , os x , apple , perl , one-liner

Safari, Chrome, and Firefox add extended attributes to files downloaded in OS X, so the OS can stupidly warn you not to open things you downloaded.  The xattr property is ‘com.apple.quarantine’.

Safari and Chrome (and maybe Firefox) also add the optional ‘com.apple.metadata:kMDItemWhereFroms’ property, which stores the URL that the file was downloaded from.  If you can get past Item Where Froms without banging your head, you’ll be pleased to know that it is stored as a Binary Property List, a binary version of the regular Property List, a long-winded XML document that Apple uses for every damn thing.

Anyway, one-liner mixture of bash and perl to display the property in a readable form:

$ xattr -p com.apple.metadata:kMDItemWhereFroms $$YOUR_FILENAME_HERE$$ |tr -d ' \n' | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' |plutil -convert xml1 - -o -

UPDATE : Again, without perl.  I didn’t know about xxd.

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

UPDATE 2 : Err, yeah. There it is.

$ mdls -name kMDItemWhereFroms $$YOUR_FILENAME_HERE$$