#!/usr/bin/env ruby # ########################################################## # SEND_SELECTED_PHOTOS_TO_WORDPRESS.RB - 31 Dec 2008 # I take your selected iPhoto images and upload them to your WordPress blog into a draft post. ########################################################## # # WRITTEN BY: Garrick Van Buren (http://garrickvanburen.com) # THANKS TO Peter Rukavina's (www.ruk.ca) iPhotoToDrupal Applesript for inspiration. # 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/ # ######################################################### # HOW TO INSTALL ME ######################################################### # # 1. I require the 'rb-appscript' rubygem. # To install rb-appscript, open terminal.app and run: sudo gem install 'rb-appscript' # For more information on rb-appscript: http://appscript.sourceforge.net # # 2. What's the url of your wordpress blog, name, and password? wordpress_url = "http://[YOUR-BLOG-HERE.COM]" name = "[YOUR-ACCOUNT-NAME]" pass = "[YOUR-ACCOUTN-PASSWORD]" # # 3. Copy me to /Library/Scripts/iPhoto (create this directory if it doesn't exist) # # 4. Change my permissions, so I'll run from your Script's menu: # Open Terminal.app and run: chmod a+x Library/Scripts/iPhoto/Send_Selected_Photos_To_WordPress.rb # # 5. You may need to modify your xmlrpc.php file for the HTML tags to post correctly. # See http://garrickvanburen.com/archive/wordpress-27-xmlrpcphp-monkey-patches for more details. # ########################################################## # HOW TO USE ME ######################################################### # # 1. Select images in iPhoto # 2. Select this script from your Script Menu # 3. Wait for your favorite browser to open up the draft post, ready for you to edit and post. # ########################################################## # THINGS IN NEED TO DO ########################################################## # - Get Some Status Messages # ######################################################### # YOU SHOULDN'T NEED TO CHANGE ANYTHING BELOW THIS LINE # ######################################################### require "rubygems" require "xmlrpc/client" require "appscript" include Appscript post = "" title = "" iPhoto = app.by_name('iPhoto') server = XMLRPC::Client.new2("#{wordpress_url}/xmlrpc.php", "", 120) # GET THE SELECTED IMAGES FROM IPHOTO the_images = iPhoto.selection.get # CYCLE THROUGH THE SELECTED IMAGES AND UPLOAD THEM TO WORDPRESS the_images.each do |new_image| # GET THE PATH TO THE IMAGE the_file_path = new_image.image_path.get # GRAB THE IMAGES FILE NAME the_file_name = the_file_path.split('/')[-1] # READ THE FILE, BASE64 ENCODE IT, AND UPLOAD IT TO WORDPRESS the_file = open(the_file_path).read the_bits = XMLRPC::Base64.new(the_file) result = server.call("metaWeblog.newMediaObject", 1, name, pass, {:name => the_file_name, :type => "image/jpeg", :bits => the_bits} ) # UPDATE THE POST AND TITLE TO INCLUDE THIS IMAGE post << "<a href=\"#{result['url']}\"><img src=\"#{result['url']}\" alt=\"#{the_file_name}\" width=\"300\" class=\"alignnone size-medium\" /></a>" title << the_file_name << " " end # CREATE A DRAFT POST CONTAINING ALL THE IMAGES result = server.call('metaWeblog.newPost', 1, name, pass, {:title => title, :description => post}, false) # LOAD UP THE DRAFT POST IN YOUR DEFAULT BROWSER app('Finder').open_location("#{wordpress_url}/wp-admin/post.php?action=edit&post=#{result.match(/\d*/)}")