#!/usr/bin/env ruby # ########################################################## # QSPRESS.RB - 02 Jan 2009 # I post to WordPress (wordpress.org) from Quicksilver (http://docs.blacktree.com/quicksilver/what_is_quicksilver) ########################################################## # # WRITTEN BY: Garrick Van Buren (http://garrickvanburen.com) # 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' and the 'mime-types' rubygems. # To install rb-appscript, open terminal.app and run: sudo gem install 'rb-appscript' # For more information on rb-appscript: http://appscript.sourceforge.net # To install mime-types, open terminal.app and run: sudo gem install 'mime-types' # For more information on mime-types: http://mime-types.rubyforge.org/ # # 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/Application Support/Quicksilver/Actions (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/Application\ Support/Quicksilver/Actions/QSPress.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. Activate Quicksilver # 2. Hit '.' to activate the text area # 3. Type your message # To apply categories: type a pipe '|' after your post, and type the categories. # Separate multiple categories with a comma. # Type another pipe '|' and 'd' if you'd prefer to save - and open - the post as a draft, rather than automatically publish it. # Examples: # "hello world" - creates a published post with a title and body of "hello world". # "hello world | general, greeting" - creates a published post with a title and body of "hello world" and categories "general" and "greeting". # "hello world | general, greeting | d" - creates a draft post with a title and body of "hello world" and categories "general" and "greeting". # "hello world || d" - creates a draft post with a title and body of "hello world". # 4. Hit the Tab key and type 'qspress' # 5. Hit Enter # ########################################################## # 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 "mime/types" require "appscript" include Appscript post = "" title = "" # SET UP THE CONNECTION TO THE WEBLOG server = XMLRPC::Client.new2("#{wordpress_url}/xmlrpc.php", "", 120) # GRAB THE INPUT FROM QUICKSILVER OR THE COMMAND LINE rawText = ARGV.join(' ') # IF THE INPUT IS A LIST OF FILES, ENCODE, AND UPLOAD THEM if ARGV[0].match(/^(\/.*)/) ARGV.each do |the_file| the_path = the_file #.file_path.get the_name = the_path.split('/')[-1] the_type = MIME::Types.type_for(the_name).first.to_s the_file = open(the_path).read the_bits = XMLRPC::Base64.new(the_file) file_result = server.call("metaWeblog.newMediaObject", 1, name, pass, {:name => the_name, :type => the_type, :bits => the_bits} ) # UPDATE THE POST AND TITLE INCLUDE THIS FILE post << "<a href=\"#{file_result['url']}\">#{the_name}</a>" title << the_name << " " end end # IF THE INPUT ISN'T A LIST OF FILES, GRAB THE TEXT TO USE FOR THE POST AND TITLE if post.empty? post = rawText.split('|')[0] title = post.slice(0, 80) end # PULL OUT ANY CATEGORIES FROM THE POST theCategories = [] theCategories = rawText.split('|')[1].split(',') if rawText.split('|')[1] # CREATE DRAFT POST OR AUTOMATICALLY PUBLISH? # AUTOMATICALLY PUBLISH IS THE DEFAULT rawText.split('|')[2] ? auto_publish = false : auto_publish = true # HANDLE ANY CATEGORIES post_categories = [] unless theCategories.empty? # GET THE EXISTING CATEGORIES existing_categories = server.call('metaWeblog.getCategories', 1, name, pass) theCategories.each do |category| category.strip! unless category.empty? # ADD ANY NEW ONES NEEDED server.call('wp.newCategory', 1, name, pass, {:name => category}) unless existing_categories.include?(category) post_categories << category end end end result = server.call('metaWeblog.newPost', 1, name, pass, {:title => title, :description => post, :categories => post_categories}, auto_publish) # 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*/)}") unless auto_publish