【发布时间】:2015-03-01 00:52:20
【问题描述】:
我想在 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 中执行此操作的示例,例如 this one:
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。
【问题讨论】:
标签: macos scripting osx-yosemite javascript-automation