Tonight’s Hack

So in preparation for the upcoming Mindcamp, I wanted to glue together the code I have to upload to flickr and Apple’s folder actions to make PhotoBooth automatically upload pictures to Flickr and put them in a PhotoBooth set.

To keep the applescript as small and troublefree as possible, I took an existing applescript folder action (in this case, copy to jpeg) and hacked it up to call a python shell script where I make a few calls using the FlickrAPI module.

-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"JPEG"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text"}, NOT: {".txt", ".text"} 
property extension_list : {"jpg", "jpeg"}

on adding folder items to this_folder after receiving these_items
    repeat with i from 1 to number of items in these_items
      set this_item to item i of these_items
      set the item_info to the info for this_item
      if (alias of the item_info is false and the file type 
          of the item_info is in the type_list) or 
          (the name extension of the item_info is in 
          the extension_list) then
        set theCmd to "/Users/erics/bin/pb_flickr.py " 
          & (quoted form of POSIX path of 
          (this_item as string)) & "&"
        --display dialog (theCmd as string)
        do shell script theCmd
      end if
    end repeat
  on error error_message number error_number
    if the error_number is not -128 then
      tell application "Finder"
        display dialog error_message buttons 
          {"Cancel"} default button 1 giving up after 120
      end tell
    end if
  end try
end adding folder items to

The python code is pretty straight forward, the only real tricks are getting api keys, secrets, and tokens. I’m not sure I remember what I did to get those, since I did it a year ago.


import os
import sys
from flickrapi import FlickrAPI

set_id='72157594368688426' # your set id here...

class flickrprefs:
  api_key = 'your key here'
  api_secret = 'your secret herer'
  auth_token = 'your token here'

if __name__=='__main__':
  argv = sys.argv
  if len(argv) < 2:
  pth = argv[1]
  prefs = flickrprefs()
    fapi = FlickrAPI(prefs.api_key, prefs.api_secret)
    rsp = fapi.auth_checkToken(api_key=prefs.api_key,
    if not rsp:
      #token isn't valid.
    rsp = fapi.upload(filename = pth, 
                      description = '', 
                      api_key= prefs.api_key,
                      auth_token = prefs.auth_token,
    rsp = fapi.photosets_addPhoto(auth_token=prefs.auth_token, 
                                   api_key= prefs.api_key,
  except Exception, msg:
    #print msg
    pass # I don't really care if I'm not connected to the net. 

Finally, it's a simple matter of dropping the applescript in the ~/Library/Scripts/Folder Action Scripts directory and enabling it with the Applescript Folder Actions Setup app.

No comments

No comments yet. Be the first.

Leave a reply

You must be logged in to post a comment.