在OSX 10.11(El Capitan)上使用AppleScript发送富文本电子邮件

9
我正在尝试通过AppleScript通过默认邮件客户端发送样式化的文本电子邮件。似乎新版本的OSX无法识别其中设置的HTML内容。是否有任何解决方法适用于10.11(El Capitan)?
解决方案在10.10(Yosemite)上运作得非常好。
tell application "Mail"
    set theMessage to make new outgoing message with properties {subject:textSubject, visible:false}
    tell theMessage
        set html content to textBody
        repeat with i from 1 to count toNameList
            make new to recipient at theMessage with properties {name:item i of toNameList, address:item i of toAddressList}
        end repeat
        send
    end tell
end tell

我遇到了同样的问题。我注意到使用“html content”发送的电子邮件主题是有的,但正文为空。没有任何内容。如果我将其更改为“content”,我会得到所有内容,但显然不会被处理为HTML,因此所有标记也可见。我怀疑Mail在El Capitan中更改了其AppleScript接口。 - hvanbrug
2
浏览Mail的AppleScript字典时,我不再看到消息和发送消息的HTML内容作为属性。我想知道他们是否因安全原因而将其删除。 - hvanbrug
1个回答

0
可以肯定地说,Mail.app 的“html content”属性已经被苹果弃用。要验证这一点,只需使用TextEdit.app查看字典文件/Applications/Mail.app/Contents/Recours/Mail.sdef。这是位置:
    <property name="html content" code="htda" type="text" access="w" hidden="yes"
 description="Does nothing at all (deprecated)"/>

但这并不意味着您不能使用写得好的HTML创建消息。

个人而言,我更喜欢不去修改正确的HTML编写方式,而是在TextEdit.app中编辑消息正文,将其保存为RTF文件,然后运行以下脚本。

-- script: Create Mail message with rich content
-- written: by @KniazidisR, posted on stackoverflow.com 22 April 2023

use framework "Foundation"
use framework "AppKit"
use scripting additions
-- classes, constants, and enums used
property NSUTF8StringEncoding : a reference to 4
property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSSharingService : a reference to current application's NSSharingService

set rtfFile to choose file with prompt "Choose the RTF file to email as HTML:" without invisibles
set theHTML to do shell script "/usr/bin/textutil -stdout -format rtf -convert html " & quoted form of POSIX path of rtfFile

set messageSubject to "My converted rtf to html"
set recipientAddresses to {"kniazidis.rompert@gmail.com"}

set theSource to NSString's stringWithString:theHTML
set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding
set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}

-- USE THE MAIL SHARING SERVICE TO CREATE A NEW MAIL MESSAGE
set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:recipientAddresses then
    set aSharingService's subject to messageSubject
    set aSharingService's recipients to recipientAddresses
    tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if

注意:当然,如果您已经有了精心编写的HTML代码,就没有必要创建RTF文件。只需将此HTML直接写入theHTML脚本变量即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接