So my Downloads folder was getting a little messy, and I’m a file system fascist, so that just didn’t sit well with me.  DOWN WITH DISORDER! 

I initially tried my hand at an Automator-generated Folder Action, but I found that as soon as Firefox starts saving a file, the AppleScript would launch as an application, taking the focus away from the browser, run for about 5s, then quit, but not return focus to the front-most browser window. Since I only ever use one browser window, this was doubly annoying.

Having mucked about with OS X’s launchd, I thought, “Why don’t I just create a stupid shell script that does this whenever my Downloads directory is modified?”

I give you… SortDownloads.

Now for the caveats.

  • I use dropbox to get the my rcrd lbl downloads to my home machine. Now when I download on the laptop, they’ll automatically be whisked away into the dropbox, where I can import it on the home machine that has my iTunes library on it.
  • I have a couple of folders in my Downloads folder. I use it as a staging area for stuff that I don’t necessarily want to put into my permanent directory structure.
    • DiskImages
    • Media
    • Media/Images
    • Media/Movies
    • Media/PDFs
    • Zips

#! /bin/bash

# Some silly variables
ho=’/Users/dork’
dl=$ho’/Downloads’
db=$ho’/Dropbox’ # http://www.getdropbox.com/ (srsly)

# Now move all sorts of stuff into folders
mv $dl/*.mp3 $db/Misc/
mv $dl/*.iso $dl/*.dmg $dl/DiskImages/
mv $dl/*.mpg $dl/*.mov $dl/*.avi $dl/*.flv $dl/*.mp4 $dl/Media/Movies/
mv $dl/*.pdf $dl/*.doc $dl/Media/PDFs
mv $dl/*.jpg $dl/*.jpeg $dl/*.gif $dl/*.png $dl/tiff $dl/Media/Images/
mv $dl/*.zip $dl/*.gz $dl/Zips

So that will do the sorting, and you can run that from the command line whenever you want to, but the real beauty is having launchd run it for you whenever the Downloads directory changes. There’s a great OS X app called Lingon that’ll make them for you, and here’s what the result looks like.

<?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″>
<dict>
<key>Label</key>
<string>com.macdork.SortDownloads</string>
<key>ProgramArguments</key>
<array>
<string>/Users/dork/Documents/Tech/Scripts/SortDownloads</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/Users/dork/Downloads</string>
</array>
</dict>
</plist>

This file ends up in your Library/LaunchAgents folder (/Users/dork/Library/LaunchAgents), and for simplicity, Lingon tells you to logout/login for the change to take effect, but you can open Terminal and run this command to do the same thing:

launchctl load ~/Library/LaunchAgents

That’s it! You’re good to go!