Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@szk-3

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

More than 5 years have passed since last update.
  • 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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
szk-3

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?