2008/02/23

rubyosa-Mail.Application

[2010-02-06 追記] RubyOSA はもはやメンテナンスされていないようで幻に包まれています。2010年2月現在においては MacRuby が熱いようです。

rubyosa からの mail.app の操作を試してみた。
メール本文のテキストの取り方が変更された様子。
# OS X 10.4 では message.content だった
rubyosa-0.3.0 :: http://www.midore.net/daybook/2007/03/1173944973.html

10.5 の Mail.app の .content はさらに階層化されててわかりづらい。
# message_viewers から辿るのではない異なるアプローチは試してない。

.content のかわりに、.properties を覗いてみるとハッシュになっておりその中に :content があるのをみつけたので
.properties[:content]
としたらうまくとれた。しかし本来 properties をみにいくのはあまりいいことではないのかもしれない。
OSA.utf8_strings = true にしていることで日本語のメール本文も正しく表示できているのだと思う。推測です。

test_1052-rubyosa-mail.rb

# this file is utf-8
# ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0] (Mac OS X 10.5.2)
# rubyosa-0.4.0
# AppleScript's version "2.0"
# Mail.Application version 3.2("919/919.2")
# 2008-02-23

require 'rbosa'

class Message < Struct.new(:subject, :postdate, :sender, :content, :messageid)
  def date(obj)
    #t = obj.date_received # Received Date
    t = obj.date_sent      # Posted Date
    t.strftime("%Y/%m/%d %H:%M:%S") # t.class is Date
  end
  def set(obj)
    # messageid
    self.messageid = obj.message_id
    # Subject
    self.subject = obj.subject
    # Date
    self.postdate = date(obj)
    # Sender
    self.sender = obj.sender
    # Message Body
    self.content = obj.properties[:content]
    ############ other items
    #obj.all_headers # String / Headers of message
    #obj.deleted_status? # FalseClass or TrueClass / message status
    #obj.message_size # Fixnum / mail size
    #obj.mail_attachments # Object /
    #obj.content # Object /
    #obj.content.paragraphs # Object /
    #obj.source # String / Message Source
    ############
  end
  def to_s
    str = ''
    self.each_pair{|k,v| str << v + "\n"}
    return str
  end
end

OSA.utf8_strings = true

mail = OSA.app('Mail')
mail.message_viewers.each{|x|
  x.selected_messages.each{|i|
    m = Message.new()
    m.set(i)
    puts m.to_s
  }
}


メールメッセージを選択してない場合のエラーなどエラーに対する処理は全て未対処。Mail.app が起動していなかった場合は下記のエラーになる。

RuntimeError: Cannot send Apple Event 'corecnte' : procNotFound (-600)


+++ memo +++
TigerLeopard(10.5) では、FileVault(システム環境)のオン<del>オン</del>オフに関係なく rubyosa 動くようになっていた。

RubyOSA
http://developer.apple.com/documentation/Cocoa/Conceptual/RubyPythonCocoa/Articles/Using%20RubyOSA.html

--imported_from
http://www.midore.net/daybook/2008/02/1203696485.html

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

0 件のコメント: