Monday, August 18, 2008

Automating Photo uploads in Blogger

This post is going to be somewhat technical. (Mom, you can stop reading right now - you're not missing anything.) I'm going to describe how to automate the uploading of photos to Blogger such that it is fairly easy to then compose a blog entry referencing those photos. This process assumes you have a Mac (since it uses AppleScript) and an FTP account on a server where you will be hosting your images. It also assumes you have the program Graphic Converter and the Cyber Duck FTP client.

First, some background.

I recently discovered that when I use the normal Blogger interface for uploading photos, Blogger is doing some sort of color conversion with the images. What I see after I upload an image is not the same as what I originally sent. Since most of the images I upload to this blog are photos, having the color changed on me - even slightly - is somewhat of a problem. I'm not claiming to have any artistic vision, but I would like what I try to display be displayed in the most accurate manner possible.

So, to get around this problem, I decided to resort to hosting my photos on my own FTP server. The problem with this is I can no longer use the convenient feature in Blogger for inserting an image in my post. I now have to do my own conversion of the photos, making a separate thumbnail image along with the larger sized image, upload those images to the FTP server and then remember the path for the images so when I craft the custom HTML code to insert the images in my blog post, the path correctly references the thumbnail and links to the larger image. Then, if I am including several photos in my blog post, I have to repeat this process for each photo! Ug! That would take forever! And I'm a slow enough writer as it is.

Fortunately, computers don't mind doing repetitive tasks such as what I just described and in fact they can do it quickly and error free. All I needed to do was write a small script to perform the proper actions. Here's a link to the sample script. It takes a list of photos, generates thumbnails of them, uploads both the thumbnails and original images to my FTP server, and then inserts into my clipboard the HTML text necessary to reference these images in my blog posting. Oh, and then the script deletes the original photos and thumbnails from my computer. That's an important point to know, especially if you are planning on using this script. THE SCRIPT IF USED AS IS WILL DELETE YOUR ORIGINAL PHOTOS! For me this is what I want since the originals are just low-res exports from my photo management system.

I should also note that the script I am using is based on a script written by Jerry Stratton. His excellent detailed description of using AppleScript with GraphicConverter can be found here. When I first needed to perform this scripting task I had no experience with AppleScript and Jerry's example was just what I needed. If you aren't too familiar with AppleScript, I'd recommend first checking out Jerry's site. He provides much more detail in the description of his script and how it works.

Here's the normal workflow for me using this script. I am using Adobe Lightroom for managing my photos. When I have a set of photos I'd like to upload to my blog, I choose to export them. I export them in a lower resolution for the web - usually with a max width or height of 600 pixels. Since these low-res photos will be deleted once I'm done, I export them to a scratch directory called blog-drop.



At the very bottom of the export dialog window there is an option to perform Post-processing" on the exported images. This is where you will choose to run the AppleScript to create thumbnails and upload the images to the FTP server.



Once you export the photos, your mouse cursor will turn into a black and white spinning wheel while the AppleScript runs. After it is complete, you can then go to the Blogger post editor and simply hit the paste key and the HTML code for referencing the just uploaded photos will automatically appear.



And that's about it as far as using the script. Don't forget, as written, the script WILL DELETE YOUR PHOTOS!!! If you don't want that to happen, look for the following lines in the script:

    -- Send all the processed files and then delete them
    ftpSend(thumbNails, destinationThumbFolder, 1)
    ftpSend(sourceFiles, destinationImageFolder, 1)

If you don't want the files to be deleted after they are FTP'd to the server, change the last argument of 1 to a 0.

One other note - the call to open an FTP connection looks like this:

    connect to hostName as user userName with protocol ftpProtocol with initial folder dest

You may notice that there is no password listed in this. I am using the feature of CyberDuck that allows saving the FTP password to the Mac keychain. This is much more secure than hardcoding the password into the script. If someone looks at your script, they won't know what the FTP password is. Of course, it could be argued that the use of FTP itself is not secure and I'd agree. Using plain FTP your username and password are sent over the Internet as clear text. If you are concerned about such things, then use SFTP instead (assuming your FTP server supports it).