-- iPhotoToDrupal AppleScript 0.3 -- Takes the selected photo(s) in iPhoto, grab the file location, title and comments -- and upload via calls to cRUL to a Drupal image gallery. -- CHANGELOG -- 0.3 Removed requirement for TEC OSAX and added some basic error checking -- 0.2 Moved all functionality into the AppleScript, allows multi-image selection -- 0.1 Initial release -- TODO -- Find a way to grab the Drupal image gallery taxonomy and prompt for a Topic for the photos -- Better error checking -- Can this become an iPhoto "Export Plugin" ? -- Created by Peter Rukavina (www.ruk.ca) -- See http://ruk.ca/archives/categories/drupal for more information. -- This work is licensed under the Creative Commons Attribution-ShareAlike License. -- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or -- send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. -- You need to set these to values for your local site set DrupalUsername to "username" set DrupalPassword to "password" set DrupalSite to "http://www.example.com/" set DrupalLoginPage to "user/login" set DrupalImagePage to "node/add/image" set CookieFile to "/tmp/curl-cookies.txt" -- You shouldn't need to change anything from here on... set curlScript to "curl " & DrupalSite & DrupalLoginPage & " -F 'edit[name]=" & DrupalUsername & "' -F 'edit[pass]=" & DrupalPassword & "' -F 'edit[destination]=" & DrupalLoginPage & "' -F 'op=Log in' --write-out '%{http_code}' --output /dev/null -s -c " & CookieFile & " -b " & CookieFile set curlReturn to do shell script curlScript -- Drupal returns HTTP return code 302 on a successful login if (curlReturn is not equal to "302") then error "Login to Drupal for user " & DrupalUsername & " failed. [Error returned was " & curlReturn & "]" end if tell application "iPhoto" activate try copy (my selected_images()) to these_images -- Make sure we one or more image selected, and if we do, then grab the details of each and upload if these_images is false or (the count of these_images) is 0 then ¬ error "Please select one or more images." repeat with this_image in these_images set this_imagefile to ((image path of this_image) as POSIX file) set this_uploadfile to (POSIX path of this_imagefile) set this_comment to (the comment of this_image) as string set this_name to the title of this_image -- variable to hold the string we're going to assemble with photo information set curlPostData to "" as string set curlPostData to curlPostData & " -F 'edit[image]=@" & this_uploadfile & "'" set curlPostData to curlPostData & " -F 'edit[name]=" & DrupalUsername & "'" set curlPostData to curlPostData & " -F 'edit[title]=" & this_name & "'" set curlPostData to curlPostData & " -F 'edit[body]=" & this_comment & "'" set curlPostData to curlPostData & " -F 'edit[created]=" & (do shell script "date +%s") & "'" set curlPostData to curlPostData & " -F 'edit[date]=" & (do shell script "date +'%Y-%m-%d %H:%M %z'") & "'" set curlPostData to curlPostData & " -F 'edit[status]=1" & "'" set curlPostData to curlPostData & " -F 'edit[moderate]=0" & "'" set curlPostData to curlPostData & " -F 'edit[promote]=0" & "'" set curlPostData to curlPostData & " -F 'edit[sticky]=0" & "'" set curlPostData to curlPostData & " -F 'edit[revision]=0" & "'" set curlPostData to curlPostData & " -F 'edit[comment]=2" & "'" set curlPostData to curlPostData & " -F 'edit[path]=" & "'" -- You need to set this to the number of your Topic; View Source on the Add an Image page and look at the SELECT list for your taxonomy to find the right value set curlPostData to curlPostData & " -F 'edit[taxonomy][]=51" & "'" set curlPostData to curlPostData & " -F 'MAX_FILE_SIZE=1000000" & "'" set curlPostData to curlPostData & " -F 'edit[existing]=" & "'" set curlPostData to curlPostData & " -F 'edit[image_id]=" & "'" set curlPostData to curlPostData & " -F 'edit[extension]=" & "'" set curlPostData to curlPostData & " -F 'edit[uid]=1" & "'" set curlPostData to curlPostData & " -F 'edit[type]=image" & "'" set curlPostData to curlPostData & " -F 'op=Submit" & "'" set curlScript to "curl " & DrupalSite & DrupalImagePage & " " & curlPostData & " --write-out '%{http_code}' -s -c " & CookieFile & " -b " & CookieFile set curlReturn to do shell script curlScript -- Drupal returns HTTP return code 302 on a successful upload if (curlReturn is not equal to "302") then error "Upload of photo '" & this_name & "' failed. [Error returned was " & curlReturn & "]" end if end repeat on error error_message number error_number if the error_number is not -128 then display dialog error_message buttons {"Cancel"} default button 1 end if end try end tell -- Taken right out the "Show Image File" AppleScript on the Apple site: -- http://www.apple.com/applescript/iphoto/ on selected_images() tell application "iPhoto" try -- get selection set these_items to the selection -- check for single album selected if the class of item 1 of these_items is album then error -- return the list of selected photos return these_items on error return false end try end tell end selected_images