我想在 OS X Yosemite 中使用 Javascript for Automation 在 Mail.app 中创建新电子邮件并将文件附加到电子邮件中。这是我的代码:
Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "abc@example.com" }))
message.subject = "Test subject"
message.content = "Lorem ipsum dolor sit"
到目前为止,它工作正常。我看到一个新的消息窗口,其中正确填写了收件人、主题和正文。但我不知道如何在消息中添加文件附件。Mail.app 的脚本字典表明该contents
属性( 的实例RichText
)可以包含附件,但我不知道如何添加。
我试过这个,但我得到一个错误:
// This doesn't work.
attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.content.attachments = [ attachment ]
// Error: Can't convert types.
我在网上找到了几个如何在 AppleScript 中执行此操作的示例,例如这个:
tell application "Mail"
...
set theAttachmentFile to "Macintosh HD:Users:moligaloo:Downloads:attachment.pdf"
set msg to make new outgoing message with properties {subject: theSubject, content: theContent, visible:true}
tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
end tell
但我不知道如何将其转换为 Javascript。