在 Applescript 中解码从 OSX Mail 原始源电子邮件返回的文本

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

我有一个 applescript 脚本,可以从 OSX Mail 中的电子邮件的原始源中提取一段文本。效果很好。

但是,原始源电子邮件中的文本经过编码并由 applescript 以相同的格式返回

有没有办法让苹果脚本转换/解码返回的文本?

以下是原始源电子邮件中文本的两个示例,Applescript 以相同的格式返回:

That's great thank you, I've just replied

还没=E2=80=99t可用

text character-encoding applescript converters decode
1个回答
0
投票

这是对我有用的解决方案 - 由 Nigel_Garvey 在 macscripter 上从原始源电子邮件中解码文本

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
-- use scripting additions
set |⌘| to current application
set input to |⌘|'s class "NSString"'s stringWithString:(input)
-- Replace any line breaks in the text with HTML versions.
set input to input's stringByReplacingOccurrencesOfString:("\\R") withString:("<br/>") ¬
    options:(|⌘|'s NSRegularExpressionSearch) range:({0, input's |length|()})
-- Convert the "HTML" string to data and thence to an attributed string.
set HTMLData to input's dataUsingEncoding:(|⌘|'s NSUTF8StringEncoding)
set attributedString to |⌘|'s class "NSAttributedString"'s alloc()'s initWithHTML:(HTMLData) ¬
    documentAttributes:(missing value)
-- Extract a string with the HTML entities interpreted.
set output to attributedString's |string|()
-- Percent encode any existing percent signs in the string.
set output to output's stringByReplacingOccurrencesOfString:("%") withString:("%25")
-- Convert any apparent "equals" encoding to percent encoding.
set output to output's stringByReplacingOccurrencesOfString:("=([0-9A-Fa-f]{2})") withString:("%$1") ¬
    options:(|⌘|'s NSRegularExpressionSearch) range:({0, output's |length|()})
-- Interpret the percent encoding and return.
return (output's stringByRemovingPercentEncoding()) as text
© www.soinside.com 2019 - 2024. All rights reserved.