Hmm… Aha…

stuff might interest somebody

Add ‘Where from:’ to Firefox downloads (Mac OS X)

Posted by f3lix on January 9, 2009

If you used Safari before you switched to Firefox, you might miss Safari’s feature that annotates each download with the URL it was downloaded from.  So if you find file in Download folder and wonder where it came from, you use Finder (“File > Get Info” or CMD+I) to show it’s source URL. Or if you like the shell, you can use the mdls command-line tool to get the info:

#> /usr/bin/mdls -name kMDItemWhereFroms a_downloaded_file.pdf
kMDItemWhereFroms = "http://www.example.com/paper.pdf"

Manually setting the “Where From:” URL

Today I asked myself if there was also a way to write this data. I did a little research on the web and found out the “Where From”-URL is actually saved in as an extended attribute. Extended attributes can be manipulated with the xattr command. So you can specify the URL for a file by running

/usr/bin/xattr -w com.apple.metadata:kMDItemWhereFroms 'http://www.example.com' <filename>

Make Firefox to set kMDItemWhereFroms

Now, what we really want is to have Firefox set the kMDItemWhereFroms property for us automatically. As I’m not familiar with programming Firefox extensions. I decided to modify an existing one: Download Statusbar 0.9.6.3. I only had to write the following function, add it to the Downbar.prototype in downbar.js and call it from the db_finishDownload function.

    db_SetMetadata: function(aDownload) {
        var cmd = '/usr/bin/xattr'
        var execFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
        var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
        
        // Arguments must be separated into an array
        var args = ['-w', 'com.apple.metadata:kMDItemWhereFroms', "'"+aDownload.source.spec+"'", aDownload.targetFile.path ];
        
        execFile.initWithPath(cmd);
        if (execFile.exists()) {
            process.init(execFile);
            process.run(false, args, args.length);
        }
    }

You can also download a patch at http://pastebin.com/f3f4ef81


Edit: for download bar 0.9.7.1 the patch is a tiny bit different. see http://pastebin.com/PbtdGNSU


About these ads

7 Responses to “Add ‘Where from:’ to Firefox downloads (Mac OS X)”

  1. moshisumo said

    thanx f3lix,
    long time since i’m looking for it
    i will relay it
    great stuff

  2. Victor Volle said

    Hi,

    have you asked the developer of Download Statusbar to add this patch to the normal distribution?

    Victor

    • f3lix said

      @Victor:

      No, I didn’t contact the developer, because I thought what I did was a nasty hack and adding it to the normal distribution never crossed my mind.

      So, what I did was to create a blog and put it there… and then forgot that I had this blog :-) until I needed my patch and I couldn’t find it on my computer …

      Greets,
      Felix R.

  3. dada said

    hi

    does not works with the last version of download statusbar 0.9.7.1

    any help

    regards

  4. ernstkm said

    (At least in recent versions of OS X, e.g., Lion), the WhereFroms metadata appears to be an array of strings. So the instructions here appear to work in that the “Where from:” shows up in Finder’s “Get Info” dialog box, but may cause unexpected behavior (or crashes) in other applications that try to read out the metadata (namely, Hazel).

    The solution I came up with is to wrap the appropriate array structure around the “aDownload.source.spec” when building up the ‘args’ array as shown above. Here’s what that looks like (and hopefully this makes it through the WordPress comment editor):

    
    // Arguments must be separated into an array
    var args = ['-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>'
      + aDownload.source.spec + '</string></array></plist>',
      aDownload.targetFile.path ];
    

    Here’s a pastebin version of that.

  5. [...] following works too, even though is has different results when running the above commands [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: