在 OSX 10.11 (El Capitan) 上具有丰富内容的 AppleScript 电子邮件

问题描述 投票:0回答:1

我正在尝试通过默认邮件客户端通过 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
email applescript html-email osx-yosemite osx-elcapitan
1个回答
0
投票

可以肯定地说,Mail.app 的html 内容 属性已被 Apple 弃用。要验证这一点,使用 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 RTF file's contents
-- 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 {"[email protected]"}

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 脚本变量即可。

© www.soinside.com 2019 - 2024. All rights reserved.