#!/usr/local/bin/ruby require 'rubygems' require 'appscript' require 'xmlrpc/client' require 'RMagick' include Magick # アカウント定義 accounts = { "hoge" => { "id" => "YOUR_ACCOUNT_NAME", "blogid" => YOUR_BLOG_ID, "password" => "YOUR_PASSWORD", "endpoint" => "app.cocolog-nifty.com" }, "hage" => { "id" => "YOUR_ACCOUNT_NAME", "blogid" => YOUR_BLOG_ID, "password" => "YOUR_PASSWORD", "endpoint" => "app.cocolog-nifty.com" } } # 引数でaccountが指定されていなければエラー if ARGV.length != 1 puts "USAGE: ./cocolog_image_uploader_variant.rb " exit(1) end # 引数で指定されたaccountが存在しなければエラー if !accounts.has_key?(ARGV.first) puts "ERROR: Specified account does not found." exit(1) else account = accounts[ARGV.first] end urls = [] filenames = [] server = XMLRPC::Client.new(account["endpoint"], "/t/api") temp_dir = File.join(ENV['HOME'], "Desktop/temp_#{$$.to_s}") Dir.mkdir(temp_dir) # iPhoto 6 (6.0.6)で確認 iphoto = Appscript.app('iPhoto') selection = iphoto.selection.get() # iPhotoで画像が選択されていなければエラー # 画像を選択していないとライブラリが選択された状態になるので # image_path.get()ができなくてエラーになる selection.each do |item| begin filenames << item.image_path.get() rescue puts "ERROR: Selected image does not found." exit(1) end end filenames.each do |filename| filename.chomp! basename = File.basename(filename) path = File.join(temp_dir, File.basename(filename)) img = ImageList.new(filename) width = 350 height = (img.rows.to_f * width.to_f / img.columns.to_f).to_i img = img.resize(width, height) img.write(path) puts "resize ok #{path}" f = XMLRPC::Base64.new(File.read(path)) result = server.call("metaWeblog.newMediaObject", account["blogid"], account["id"], account["password"], {"bits" => f, "name" => "xmlrpc_images/#{Time.now.strftime("%Y-%m-%d")}/#{basename}"}) puts "upload ok #{result["url"]}" urls.push(result["url"]) File.delete(path) puts "delete ok #{path}" end Dir.rmdir(temp_dir) html = "" urls.each{|url| html += '' + "\n\n\n" } content = { "title" => "Temporary Entry #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}", "description" => html } result = server.call("metaWeblog.newPost", account["blogid"], account["id"], account["password"], content, 0) puts "entry is posted." edit_url = "https://#{account["endpoint"]}/t/app/weblog/post?__mode=edit_entry&id=#{result}&blog_id=#{account["blogid"]}" puts edit_url system("open '#{edit_url}'")