2007/03/15

rubyosa-0.3.0

rubyosa 0.3.0が公開されていた。(2007-03-03付け)
付属していたサンプルに「get_selected_mail.rb」というのがあったので試してみた。
Mail.app の受信メールメッセージを選択している時にこのスクリプトを動かすとテキストエディットが起動し選択していたメール本文内容がテキストエディットの本文に表示されるものだった。問題なく動いた。が、若干問題があった。
試したサンプル「get_selected_mail.rb」の中身。

%cat get_selected_mail.rb

# Retrieve and show every selected message content in Mail into new TextEdit documents. 

begin require 'rubygems'; rescue LoadError; end
require 'rbosa'

OSA.utf8_strings = true
textedit = OSA.app('TextEdit')
mailApp = OSA.app('Mail')
viewers = mailApp.message_viewers
viewers.each do |viewer|
  viewer.selected_messages.each do |message|
    textedit.make(OSA::TextEdit::Document).text = message.content
  end
end


英語の場合は全く問題ないけれど日本語で書かれたメール(Content-Type: text/plain; charset="ISO-2022-JP")の時は文字化けする。
OSA.utf8_strings = true となっているが効力を発揮していないようだ。発揮しているけれど途中経過で打ち消されているのかもしれない。

仕方ないので下記のように書き換えてみたところ文字化けが解消された。一旦 Shift-jis にしてからTextEditorに渡すようにしてみた。TextEditor 側の環境設定はデフォルト状態でも大丈夫だしUTF-8で「開く」「保存」となっていても問題なかった。
※ utf-8 で保存しておきたい場合は環境設定を変更しておくなり、テキスト保存時にutf-8を選択する必要がある。文字によってはここで保存できない場合もあるのかもしれない。

これをAppleScriptから呼び出すようにしAppleScript側でtry-on error -end にしてエラー処理を行うようにしたので Ruby側ではbegin...endをしていない。
また、サンプルにはない機能だが表示するテキストに引用府をつけ受信時刻と受信日付送信者の情報を付け足してみた。

#  j_get_selected_mail.rb
#  encoding: utf-8
#  ruby 1.8.5 

begin require 'rubygems'; rescue LoadError; end
require 'rbosa'

require 'nkf'
require 'time'
OSA.utf8_strings = true
textedit = OSA.app('TextEdit')
mailApp = OSA.app('Mail')
viewers = mailApp.message_viewers
viewers.each do |viewer|
  viewer.selected_messages.each do |message|
    # case rubyosa v2.0
    #mesdate = message.properties['alhe'].scan(/Date.*?\n/).to_s.gsub(/Date:\s/,'').chomp
    # rubyosa v3.0
    mesdate = Time.parse(message.source.scan(/Date.*?\n/).to_s.gsub(/Date:\s/,'').chomp)#
    mes_time = mesdate.strftime("%X")
    mes_date = mesdate.strftime("%y-%m-%d")
    # Message Body
    mes_body = NKF.nkf("-s", message.content)
    # Message Sender
    mes_sender = NKF.nkf("-s", message.sender)
    # Message Body
    res_str = "On #{mes_date}, at #{mes_time}, #{mes_sender} wrote:\n\n" + ">"  + mes_body.gsub("\n", "\n>")
    # Mail Contents send 'TextEdit.App'
    textedit.make(OSA::TextEdit::Document).text = res_str
  end
end


アップルスクリプト 側

 --j_get_selected_mail.scpt
 --2007-03-05
 --AppleScript's version "1.10.7"
 --RubyOSA ver 0.3.0[2007-03-03]
 --Mail.app バージョン 2.1.1(752.3)

try
  set myrubypath to "/usr/local/bin/ruby"
  set mypath to "/Users/midori/rubyosa3/j_get_selected_mail.rb"
  
  tell application "Mail"
    activate
  end tell
  
  set response to do shell script myrubypath & "  " & mypath
  if ("error") is in (response as Unicode text) then
    display dialog "Please select message(s) of Mail.app " buttons {"OK"} default button 1
    tell application "Mail"
      activate
    end tell
    return
  else
    tell application "TextEdit"
      activate
    end tell
    return
  end if
on error
  tell application "Finder"
    activate
    display dialog "error: Please select message(s) of Mail.app.
    or your ruby path exist?
    or your mypath  exist?
    or your Applescript's Version is old?
    or your RubyOSA is old? 
    or  something error... 
    " buttons {"OK"} default button 1
    return
  end tell
end try


--telop
曇時々雨

--imported_from
http://www.midore.net/daybook/2007/03/1173944973.html

+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

0 件のコメント: