Posted at

Cocoaの機能を使ってXMLエンコード/デコードするAppleScriptハンドラ

More than 1 year has passed since last update.


encodeXML.scpt

use framework "Foundation"

set encodedXML to my encodeXML("<p>あいうえお</p>
<strong>かきくけこ</strong>"
)
(*"&lt;p&gt;あいうえお&lt;/p&gt;
&lt;strong&gt;かきくけこ&lt;/strong&gt;"
*)

on encodeXML(XML as text)
--require framework: Foundation
set attributedString to current application's NSAttributedString's alloc()'s initWithString:XML
set attributes to current application's NSDictionary's dictionaryWithObject:(current application's NSHTMLTextDocumentType) forKey:(current application's NSDocumentTypeDocumentAttribute)
set HTMLData to attributedString's dataFromRange:{location:0, |length|:attributedString's |length|} documentAttributes:attributes |error|:(missing value)
set HTMLString to current application's NSString's alloc()'s initWithData:HTMLData encoding:(current application's NSUTF8StringEncoding)
set HTMLContent to item 2 of item 1 of my regexMatches(HTMLString, "<body>\\s([\\w\\W]*)\\s</body>")
return my regexReplace(HTMLContent, "<[^>]*>", "")
end encodeXML

on regexMatches(aText as text, pattern as text)
--require framework: Foundation
set regularExpression to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value)
set aString to current application's NSString's stringWithString:aText
set matches to regularExpression's matchesInString:aString options:0 range:{location:0, |length|:aString's |length|()}

set matchResultList to {}
repeat with match in matches
set matchResult to {}
repeat with i from 0 to (match's numberOfRanges as integer) - 1
set end of matchResult to (aString's substringWithRange:(match's rangeAtIndex:i)) as text
end repeat
set end of matchResultList to matchResult
end repeat
return matchResultList
end regexMatches

on regexReplace(aText as text, pattern as text, replacement as text)
--require framework: Foundation
set regularExpression to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value)
return (regularExpression's stringByReplacingMatchesInString:aText options:0 range:{location:0, |length|:count aText} withTemplate:replacement) as text
end regexReplace



decodeXML.scpt

use framework "Foundation"

my decodeXML("&lt;p&gt;あいうえお&lt;/p&gt;
&lt;strong&gt;かきくけこ&lt;/strong&gt;"
)
(*<p>あいうえお</p>
<strong>かきくけこ</strong>*)

on decodeXML(XML as text)
set HTMLContent to my regexReplace(XML, "[\\n\\r]", "<br/>" & linefeed)
return my convertHTMLToPlainText(HTMLContent)
end decodeXML

on regexReplace(aText as text, pattern as text, replacement as text)
--require framework: Foundation
set regularExpression to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value)
return (regularExpression's stringByReplacingMatchesInString:aText options:0 range:{location:0, |length|:count aText} withTemplate:replacement) as text
end regexReplace

on convertHTMLToPlainText(HTML)
--require framework: Foundation
set attributedString to my attributedStringOfHTMLText(HTML)
return attributedString's |string| as text
end convertHTMLToPlainText

on attributedStringOfHTMLText(HTML as text)
--require framework: Foundation
set HTMLString to current application's NSString's stringWithString:HTML
set HTMLData to HTMLString's dataUsingEncoding:(current application's NSUnicodeStringEncoding)
return current application's NSAttributedString's alloc()'s initWithHTML:HTMLData documentAttributes:(missing value)
end attributedStringOfHTMLText



更新履歴


  • 2016-09-05: NSAttributedStringクラスを使って作成

  • 2017-07-21: encodeXMLハンドラにおいてregexMatchesハンドラとregexReplaceハンドラを使用