高度な検索
Developer Connection
Member Login ログイン | ご入会 ADC連絡先

Technical Q&A QA1018
Using AppleScript to send an email with an attachment


Q:どのようにすれば、Mac OS X の電子メールプログラムに添付書類付きの電子メールを送信するように要求する AppleScript を書けるでしょうか。

A:Mac OS X の電子メールプログラムは AppleScript に対応しています。リスト 1 に示すスクリプト例では、ユーザが、送信メッセージに添付するテキストファイルを選択できるようにしています。



tell application "Mail"
    (* 電子メールアプリケーションのバージョン  *)
    set mailversion to version as string

    (* 表示または送信の処理を指定 - displayForManualSend
        が true ならば、ユーザが送信できるように、メッセージが
        生成され、ウィンドウに表示されます。false の場合、
        メッセージはすぐに送られます *)
    set displayForManualSend to true

    (* メッセージの一般的な要素を指定 *)
    set bodyvar to return & return & "Test body."
    set addrVar to "bogus@apple.com"
    set addrNameVar to "Guinea Pig"

    (* 添付書類を指定 *)
    (* このファイルリストはその他の設定を示す *)
    (* 電子メールを送信するためのスクリプト手順 *)

    (* 注意:mailto 形式の URL では添付書類の *)
    (* 指定ができない *)
    set fileAttachThis to choose file of type "TEXT"
    set fileList to {fileAttachThis}

    (* 件名を定義 *)
    set subjectvar to "Test Message From AppleScript with Attachment!"

    (* メッセージを作成 *)
    set composeMessage to (a reference to (make new compose message ¬
        at beginning of compose messages))
    tell composeMessage
        make new to recipient at beginning of to recipients ¬
            with properties {address:addrVar, display name:addrNameVar}
        set the subject to subjectvar
        set the content to bodyvar
        tell content
            repeat with aFile in fileList
                make new text attachment ¬
                    with properties {file name:aFile} ¬
                    at before the first word of the ¬
                    first paragraph
            end repeat
        end tell
    end tell

    (* メッセージを送信または表示 *)
    if displayForManualSend then
        set messageEditor to make new message editor ¬
            at beginning of message editors

        (* 次に、電子メールアプリケーションのバージョン 
            1.0 と 1.1. にはあるが以降のバージョンで修正されたバグの
            回避方法を示す *)
        if mailversion is "1.0" or mailversion is "1.1" then
            set compose message of last message editor to composeMessage
        else
            set compose message of first message editor to composeMessage
        end if
    else
        send composeMessage
    end if
end tell

リスト 1 添付書類付きの電子メールを送信する AppleScript




[2002 年 1 月 4 日]