Distillr 1.0.3 - Mac OS X - AppleScript
[sorry, this page is available only in English]
Why?
If you are a "Flickr-addict"
like me, you might have a similar surfing pattern to mine -- which is to look at the first couple of pages of my contacts, begin my surf from their latest images and from there dig into their favorites and continue. And I have good reasons to believe that there are other people who are "surfing favorites" out there
.
This Flickr usage pattern is great to find new quality images all the time but it also tends to have two less desirable side effects:
- the number of contacts tends to increase rapidly,
- because of the growing number of contacts some very good one tend to fall down the list of recent images from contacts, thus being ignored.
This becomes even more obvious
- when having a limited amount of time to dedicate to Flickr (i.e. an hour after dinner)
- for contacts who tend to post at times of the day very different from the time we use Flick (i.e. a contact from Japan would be more likely to post in her evening time which corresponds to the morning office hours in the US)
What is Distillr?
After noticing this fact, I decided that, in order to avoid falling down the list of recent images on my contacts' Flickr homepages, I wanted to be able to drop a bunch of images in a folder, together with matching text files containing info about the images (title, description and associated tags) and have a scheduled job send out an email to my Flickr reserved email address - for instance every 6 hours - with one image as attachment and the description in the body of the email. Literally "distilling" my newest images on my contacts' From My Contacts page.
Because I have recently switched to OS X I had a hard time finding this functionality already done by someone else, so I decided to tackle a long-time almost-forgotten friend: Applescript (inside joke that most won't understand).
The Script
To make a long story short, here is the script:
-- DISTILLER 1.0.3
-- An automated system to send images to Flickr using emails
-- v.1.0.3 04/27/2008 (CC: Creative Commons) Francesco Gallarotti - info@gallarotti.net
-- set this to the folder where you are going to drop image files and description text files
set distillrFolderPath to \"Mac HD:Users:MyUserName:Documents:Distillr\"
-- set this to the flickr email address you have chosen to upload directly to your flickr account
set flickrEmailAddress to \"myFlickrMailAddress@photos.flickr.com\"
-- initialization stuff
set prefixLength to 3
set attachFileName to false
-- get the first available image in the Distillr folder
set currentFileName to getFirstImageFileName(distillrFolderPath)
-- if one image is available...
if currentFileName is not null then
set distillrAttachmentPath to distillrFolderPath & \":\" & currentFileName
set distillrAttachment to distillrAttachmentPath as alias
-- ...extract the first 'prefixLength' characters of its filename...
set filenamePrefix to text 1 thru prefixLength of currentFileName
-- ...and get the description file...
set descriptionFileName to filenamePrefix & \".txt\"
try
set descriptionPath to distillrFolderPath & \":\" & descriptionFileName
set distillrDescription to readFile(descriptionPath)
-- get the first line and use it as title
set flickrTitle to first paragraph of distillrDescription as text
-- get the description
set parList to paragraphs in distillrDescription
if attachFileName then
set flickrDescription to currentFileName & return & return & distillrDescription
else
set flickrDescription to distillrDescription
end if
-- send the email!
sendFlickrMail(flickrTitle, flickrDescription, distillrAttachment, \"FlickrMail\", flickrEmailAddress)
tell application \"Finder\" to delete file distillrAttachmentPath
on error errTxt number errNum
display dialog errTxt & return & errNum
end try
end if
-- this sends out the email with the image attachment
-- using the Mail application that comes with Mac OS X
-- this of course requires that Mail is correctly setup to send out emails
on sendFlickrMail(theSubject, theBody, theAttachment, theRecipientName, theRecipientEmailAddress)
tell application \"Mail\"
set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theBody}
tell content of theMessage
make new attachment with properties {file name:theAttachment} at after last paragraph
end tell
tell theMessage
make new to recipient at end of to recipients with properties {name:theRecipientName, address:theRecipientEmailAddress}
end tell
send theMessage
end tell
end sendFlickrMail
-- this gets the very first image (in alphabetical order) present in the Distillr folder
on getFirstImageFileName(folderPath)
tell application \"Finder\" to set distillrList to every file of folder folderPath
-- first let's get all the filenames sorted
set listNames to {}
repeat with distillrItem in distillrList
set listNames to listNames & {the name of distillrItem as string}
end repeat
set listNames to sort(listNames)
-- now get the first image
repeat with currentFileName in listNames
set AppleScript's text item delimiters to \".\"
set currentExtension to last text item of currentFileName
if (currentExtension = \"jpg\") then
-- found the attachment
return currentFileName
end if
end repeat
end getFirstImageFileName
-- read the content of a text file
on readFile(posixPath)
set theFile to (open for access posixPath)
set theContent to (read theFile)
close access theFile
return theContent
end readFile
-- http://www.macosxhints.com/article.php?story=20040513173003941
-- Sort lists in AppleScript using the Unix sort command
-- Mon, May 17 '04 at 09:13AM by: erickaterman
on sort(the_list)
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10}
set list_string to (the_list as string)
set new_string to do shell script \"echo \" & quoted form of list_string & \" | sort -f\"
set new_list to (paragraphs of new_string)
set AppleScript's text item delimiters to old_delims
return new_list
end sort
How to use it?
Read carefully all the comments if you are interested in understanding how it actually works. If you just want to use it, then copy and paste the code into your Script Editor (you can find it in the Applications/AppleScript folder on your Mac hard drive).
Make sure you modify the first two lines to have the correct path to your Distillr folder (the folder where you are going to drop your files) and the Flickr reserved email address that you use to upload images to your account (if you have a cellphone camera you might have already used that email address a lot, else take a look at the Flickr: Upload by Email page for more info on how to set it up. It's pretty simple.
When you drop a set of images in your folder, they will be picked up in alphabetical order. This means that if you have the following images:
abc01.jpg, cde04.jpg, abc06.jpg, abc02.jpg, cde14.jpg
they will be picked up in the following order:
abc01.jpg, abc02.jpg, abc06.jpg, cde04.jpg, cde14.jpg
Each time the script is run (manually or scheduled - we will talk about that later) the first image will be "picked" and the correspondent description text file is also picked. By default, the associated description text file is the one whose filename is equal to the first three letter of the batch of images. This means that, continuing to refer to the previous set of files, abc01.jpg, abc02.jpg, abc06.jpg are associated to abc.txt and cde04.jpg, cde14.jpg are associated to cde.txt.
If the description file is not found the script stops and nothing happens, which means that you can use this feature to prevent a batch from being sent to Flickr by simply renaming the batch description file to not match the first three letter of the images filenames.
If the description file is found, an email is sent with the first line of the text file used as the subject - therefore, the first line of the text file will become the title of all the images in the batch. The entire file will be sent in the body of the email - therefore becoming the description of every image in the batch. The image will be attached to the email (limit imposed by Flickr for these attachments is 6MB) and will be deleted from the Distillr folder! Because this has to happen to prevent the image from being picked up the next time again, please only drop in the Distillr folder copies of your images - not the originals. If something bad happen, the images can be retrieved from your trashcan
How to schedule it?
If you want to schedule your script I suggest you save the script as an application -- you can do that directly from the Script Editor. Then you can use CronniX to setup your schedules.
CronniX is an Aqua frontend to the powerful Unix tool "cron". Cron is a Unix system service that allows scheduled execution of scripts, programs, applications - in short anything that can be started from the command line. This includes OSX applications and AppleScripts.
Comments? Questions?
Post a comment here if you are having troubles setting this whole thing up. Took me a while to figure out all the steps and I hope I covered everything here in this write up. If not, I will update with the answer to the FAQs soon.
Hopefully you find this script useful!










