Automatic image resize and ftp upload with Hazel 3.1


Hazel 3.1, the latest upgrade of the awesome automated file organizer for Mac, just became available for download. I’ve been using it in beta for a while, and there’s one new feature I’m particularly enjoying which makes posting images to this blog (and other places) pretty darn easy:

New Upload Action

I wrote previously about my workflow for getting images uploaded to my FTP server using Hazel and some shell scripting.

Hazel 3.1 ships with something that makes all of that messing around with scripts a weary memory of the past: there’s a new “upload” action that allows you to upload files to FTP/SFTP/WebDAV servers.

Using this functionality, I now have a much improved Hazel workflow to resize images and upload them to my ftp server when I place them in a pre directory. (I have another rule that copies all screenshots I take to that folder.)

What’s happening here? Well, first Hazel is running an applescript to resize the image. I use ImageMagick so my script looks like this:

cd /usr/local/bin  
mogrify -adaptive-resize 650x800\> "$1"  

(Click here for an update to this script which allows you to resize iPhone images differently)

If you don’t have ImageMagick you can use the built in Image Events that OS X provides, which calls for a longer script:

tell application "Image Events"  
    set this_image to open theFile  
    set imageDimensions to dimensions of this_image  
    if item 1 of imageDimensions > 650 then  
        scale this_image to size 650  
        save this_image in theFile  
    end if  
    close this_image  
end tell  

Hazel then renames the file something unique based on the time:

…and uploads it via Hazel’s new upload action.

Then another script is run so that the new URL of the uploaded file is copied to the clipboard, ready to be used.

set file_path to theFile as text  
try  
    set text item delimiters to ":"  
    set file_name to last text item of file_path  
    set text item delimiters to ""  
on error  
    set text item delimiters to ""  
end try  
set the clipboard to "http://sidoneill.com/51d/public_html/img/" & file_name  

To finish things up I display a notification and move the file to an archive folder.

This isn’t the only new feature in Hazel 3.1. Amongst many small improvements there’s also a very cool action for pattern-matching against a file’s contents. I imagine this will be really helpful for those with a paperless workflow that includes scanning in documents for OCR.

EDIT: David Sparks already has a great video on how to do this.

If you have a Mac and you don’t have Hazel, you really should take a look. What TextExpander is for text, Hazel is for files.