OSX 10.11(El Capitan)上包含丰富内容的AppleScript电子邮件

Pra*_*ami 8 email applescript html-email osx-yosemite osx-elcapitan

我正在尝试通过AppleScript通过默认邮件客户端发送带样式的文本电子邮件.似乎新版本的OSX无法识别内部的HTML内容.10.11(El Capitan)有没有解决方法?

使用10.10(优胜美地)解决方案工作得非常好.

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
Run Code Online (Sandbox Code Playgroud)

Rob*_*dis 0

可以肯定地说, Apple 已弃用Mail.app 的html 内容属性。要验证这一点,只需使用 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)"/>
Run Code Online (Sandbox Code Playgroud)

但是,这并不意味着您不能使用编写良好的 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
Run Code Online (Sandbox Code Playgroud)

注意:当然,如果您有编写良好的 HTML 可供使用,则无需创建 RTF。只需将此 HTML 直接写入HTML脚本变量即可。