【问题标题】:Send email with attachment using Javascript for Automation使用 Javascript for Automation 发送带附件的电子邮件
【发布时间】: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


    【解决方案1】:

    我通过反复试验找到了一种方法,您需要使用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)
    

    【讨论】:

    • 是的,它是 JXA 中几个愚蠢的混淆之一。正确的语法应该Mail.make({new:k.outgoingMessage, at:message.attachments, withProperties:{visible:true,subject:"Test",content:"Foo"}}),因为Apple事件实际上就是这样工作的;即 RPC + 简单的关系查询。但是 AS 团队坚持对 JavaScript 和 Objective-C 等语言的用户撒谎,所以你必须通过这种虚假的 OOP 废话,假装(糟糕地)你正在操纵一个本地的 Array 对象而不是描述一个远程进程的对象图中的一对多关系。 #HeadDesk
    • 是的,它应该只是匹配底层概念,它们不难理解。
    • 是的。我一直用头撞墙,试图告诉 AppleScript/JXA 开发人员这一点。一旦您意识到它不是 OOP,Apple 事件 IPC 实际上很容易理解且符合逻辑。真正令人沮丧的是,如果 JXA 确实与底层概念相匹配,那么为用户提供一个自动翻译工具(参见ASTranslate)将非常容易,它将 AppleScript 命令转换为等效的 JavaScript 语法,此时 90% 的“如何将此 AppleScript 转换为 ”问题有效地回答了自己。
    • 顺便说一句,我向 AS/JXA 团队发送了一个几乎完整的 JavaScript OSA 参考实现。它的 Apple 事件支持源自旧的 appscript 桥,与 JXA 不同,它经历了数以千计的无情的轮胎踢,以及数百个个人和专业用户(尤其是我自己)对数百个可编写脚本的应用程序的实际使用。越野车和未抛光,但如果您对 JXA 可以/应该如何工作感到好奇,值得一看。也许如果有足够多的 JS 用户对 JXA 感到愤怒,他们可能会大量涌入以迫使 Apple 重做它。
    • @foo 您的代码示例中的k 是什么(上面的第一个推荐)以及为什么是at:message.attachements,为什么是at?这听起来好像应该将新邮件创建为附件。
    【解决方案2】:

    基于@tlehman 上面的答案,我发现他的解决方案效果很好,除了一件事:附件不成功。没有错误,没有消息,只是没有对消息添加附件。解决方法是使用Path() 方法。

    这是他的解决方案 + Path() 方法,如果路径中有空格,则需要:

    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: Path("/Users/myname/Desktop/if the file has spaces in it test.pdf") })
    message.attachments.push(attachment)

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 2016-10-23
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2016-09-22
      • 2019-02-25
      相关资源
      最近更新 更多