Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.85 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -- Used the Tumblr v1 API because it is light-years simpler http://www.tumblr.com/docs/en/api/v1#api_write
  2. --
  3. -- To use this, open it in the AppleScript Editor, change the properties below,
  4. --  switch to Mail and select the emails with pics attached that you wish to import to tumblr,
  5. --  switch back to the AppleScript Editor and press Run.
  6. --
  7. --  NOTE: tumblr limits your photo uploads to 75 per day, so plan accordingly.
  8. --
  9.  
  10. property tumblr_email : "----"
  11. property tumblr_password : "-----"
  12. property tumblr_group : "example.tumblr.com"
  13.  
  14. tell application "Finder" to set downloadPath to (path to downloads folder) as string
  15. set downloadPosixPath to POSIX path of downloadPath as string
  16.  
  17. tell application "Mail"
  18.         set postsCreated to 0
  19.         set postsFailed to 0
  20.         set selectedMessages to selection
  21.         set AppleScript's text item delimiters to ""
  22.         if (count of selectedMessages) is equal to 0 then
  23.                 display alert "No Messages Selected" message "Select the message you want to get the raw source of before running this script."
  24.         else
  25.                 repeat with eachMessage in selectedMessages
  26.                         set message_sent to the date sent of eachMessage
  27.                         set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to message_sent
  28.                         set post_time to y & "-" & m - 0 & "-" & d & " " & h & ":" & min & ":" & s -- TODO: blog's timezone thankfully matches
  29.                         set caption to the subject of eachMessage
  30.                         set post_data to ""
  31.                         set theOutputFolder to the downloadPath
  32.                         repeat with eachAttachment in every mail attachment of eachMessage
  33.                                 set theSavePath to theOutputFolder & name of eachAttachment
  34.                                 save eachAttachment in theSavePath
  35.                                 set post_data to post_data & " -F \"data=@" & eachAttachment's name & ";type=" & eachAttachment's MIME type & "\""
  36.                         end repeat
  37.                         set tumblr_api_script to "cd " & quoted form of downloadPosixPath & ";/usr/bin/curl" & ¬
  38.                                 " -L" & ¬
  39.                                 " -F \"email=" & tumblr_email & "\" " & ¬
  40.                                 " -F \"password=" & tumblr_password & "\" " & ¬
  41.                                 " -F \"type=photo\" " & ¬
  42.                                 " -F \"generator=AppleScript\" " & ¬
  43.                                 " -F \"date=" & post_time & "\" " & ¬
  44.                                 " -F \"group=" & tumblr_group & "\" " & ¬
  45.                                 " -F \"send_to_twitter=auto\" " & ¬
  46.                                 " -F \"caption=" & caption & "\" " & ¬
  47.                                 post_data & ¬
  48.                                 " http://www.tumblr.com/api/write "
  49.                        
  50.                         set upload_response to do shell script tumblr_api_script
  51.                        
  52.                         repeat with eachAttachment in every mail attachment of eachMessage
  53.                                 do shell script "rm " & downloadPosixPath & eachAttachment's name
  54.                         end repeat
  55.                        
  56.                         -- TODO: curl stdout output is the response body and not the headers. Try it with -v?
  57.                         if upload_response contains "201 Created" then
  58.                                 set postsCreated to postsCreated + 1
  59.                         else
  60.                                 set postsFailed to postsFailed + 1
  61.                                 log upload_response
  62.                         end if
  63.                        
  64.                 end repeat
  65.                 display alert "Posts Created" message "There were " & postsCreated & " posts created on Tumblr and " & postsFailed & " failures."
  66.         end if
  67. end tell