You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AppleScript解码OSX Mail原始邮件中的编码文本?

解决OSX Mail邮件编码文本的AppleScript解码方案

你遇到的是邮件源码里两种常见编码:HTML实体编码(如')和Quoted-Printable编码(如=E2=80=99),可以通过AppleScript结合系统工具或Python实现解码,以下是具体方案:

1. 解码HTML实体编码

AppleScript本身无内置HTML实体解码能力,可借助Python的html模块完成,示例函数:

on decodeHTMLEntities(rawText)
    return do shell script "python3 -c \"import html; print(html.unescape('" & quoted form of rawText & "'))\""
end decodeHTMLEntities

调用后,That's great thank you, I've just replied会被解码为:That's great thank you, I've just replied,中文翻译为:太好了,谢谢你,我刚回复了

2. 解码Quoted-Printable编码

这种编码属于邮件MIME标准,有两种实现方式:

方法一:纯AppleScript(依赖Foundation框架)

use framework "Foundation" -- 必须添加这句声明

on decodeQuotedPrintable(rawText)
    set rawData to rawText as data using encoding «class utf8»
    set decodedString to current application's NSString's stringWithData:rawData encoding:(current application's NSUTF8StringEncoding)
    return decodedString as text
end decodeQuotedPrintable

方法二:借助Python的quopri模块

on decodeQuotedPrintable(rawText)
    return do shell script "python3 -c \"import quopri; print(quopri.decodestring('" & quoted form of rawText & "').decode('utf-8'))\""
end decodeQuotedPrintable

调用任意函数后,It hasn=E2=80=99t been available会被解码为:It hasn’t been available,中文翻译为:它一直不可用

3. 组合两种解码逻辑

若邮件文本同时存在两种编码,可先解码Quoted-Printable,再处理HTML实体,示例脚本:

use framework "Foundation"

on decodeMailText(rawText)
    set decodedQP to decodeQuotedPrintable(rawText)
    set finalText to decodeHTMLEntities(decodedQP)
    return finalText
end decodeMailText

on decodeQuotedPrintable(rawText)
    set rawData to rawText as data using encoding «class utf8»
    set decodedString to current application's NSString's stringWithData:rawData encoding:(current application's NSUTF8StringEncoding)
    return decodedString as text
end decodeQuotedPrintable

on decodeHTMLEntities(rawText)
    return do shell script "python3 -c \"import html; print(html.unescape('" & quoted form of rawText & "'))\""
end decodeHTMLEntities

内容的提问来源于stack exchange,提问作者user24006022

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:15:05