LoginSignup
2
2

More than 5 years have passed since last update.

Cocoaの機能を使ってHTML文字列をプレーンテキストに変換するAppleScriptハンドラ

Last updated at Posted at 2016-01-27
  • do shell scriptと違って文字数制限がないので使いやすい
  • 文字化けする場合はdataUsingEncoding:メソッドの引数を変更
convertHTMLToPlainText.scpt
use scripting additions
use framework "AppKit"

tell application "Safari"
    my convertHTMLToPlainText(source of current tab of window 1)
end tell

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: AppKit, 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-01-27: CocoaのAppKitフレームワークにあるNSAttributedStringクラスを使って作成
  • 2016-02-01: attributedStringOfHTMLTextハンドラ作成
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2