我想在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: "[email protected]" }))
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。
我想出了一种通过反复试验来做到这一点的方法,您需要使用message.attachments.push(attachment)
而不是attachments = [...]
Mail = Application('com.apple.Mail')
message = Mail.OutgoingMessage().make()
message.visible = true
message.toRecipients.push(Mail.Recipient({ address: "[email protected]" }))
message.subject = "Testing JXA"
message.content = "Foo bar baz"
attachment = Mail.Attachment({ fileName: "/Users/myname/Desktop/test.pdf" })
message.attachments.push(attachment)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句