我想在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。
最佳答案
我想出了一种通过反复试验来做到这一点的方法,您需要使用message.attachments.push(attachment)
而不是attachments = [...]
Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "foo.bar@example.com" }))
message.subject = "Testing JXA"
message.content = "Foo bar baz"
attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.attachments.push(attachment)
关于macos - 使用Javascript for Automation发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27746418/