macos - 使用Javascript for Automation发送带有附件的电子邮件

标签 macos scripting osx-yosemite javascript-automation

我想在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/

相关文章:

在 Mac OS X 上找不到 C 头文件

ansible-galaxy 角色失败,错误为 "do not have permission to modify/etc/ansible/roles/"

swift - OSX swift : How to listen to Keypress?

macos - 系统存储在 macOS Mojave 中占用太多空间

linux - 在 shell 脚本中查找和替换

batch-file - 提示用户在批处理文件中选择目录

macos - NSClickGestureRecognizer 不适用于 NSStatusItem

c++ - md5.h 文件未找到错误 Xcode 7 for Mac 应用程序

HTML5 Geolocation API获取WiFi AC信息?

macos - 无法将参数传递到简单的 ShellScript(找不到命令)