Help us understand the problem. What is going on with this article?

HTML文字列と«class HTML»データを変換するAppleScriptハンドラ

More than 3 years have passed since last update.
  • AppleScriptでクリップボードを取得すると、しばしば«class HTML»というクラスのデータが出現
  • «class HTML»のデータはHTML文字列と相互変換できる

dataHTMLFromHTML

dataHTMLFromHTML.scpt
my dataHTMLFromHTML("<p>あいうえお</p>")
--> «data HTML3C703EE38182E38184E38186E38188E3818A3C2F703E»

on dataHTMLFromHTML(HTML as text)
    return do shell script "echo " & quoted form of HTML as «class HTML»
end dataHTMLFromHTML

更新履歴

  • 2011-08-14: HTML文字列のASCII numberを変換する形式で作成
  • 2012-03-13: hexdumpを使ったdo shell scriptに変更
  • 2016-01-10: echoを使ったdo shell scriptに変更

dataHTMLToHTML

dataHTMLToHTML.scpt
use scripting additions
use framework "Foundation"

my dataHTMLToHTML(«data HTML3C703EE38182E38184E38186E38188E3818A3C2F703E»)
--> "<p>あいうえお</p>"

on dataHTMLToHTML(dataHTML as «class HTML»)
    try
        dataHTML as text
    on error e
    end try
    return my doShellScript("echo " & quoted form of text 5 thru -1 of word 2 of e & " | xxd -r -p")
end dataHTMLToHTML

on doShellScript(command as text)
    --require framework: Foundation
    set task to current application's NSTask's alloc()'s init()
    set task's launchPath to "/bin/sh"
    set task's arguments to {"-c", command}
    set outPipe to current application's NSPipe's pipe()
    set task's standardOutput to outPipe
    set errorPipe to current application's NSPipe's pipe()
    set task's standardError to errorPipe
    task's |launch|()
    set outData to outPipe's fileHandleForReading()'s readDataToEndOfFile()
    if outData = missing value or outData's |length|() as integer = 0 then
        set errorData to errorPipe's fileHandleForReading()'s readDataToEndOfFile()
        if errorData = missing value or errorData's |length|() as integer = 0 then
            error "0 以外の状況でコマンドが終了しました。" number 1
        else
            error (current application's NSString's alloc()'s initWithData:errorData encoding:(current application's NSUTF8StringEncoding)) as text number 1
        end if
    end if
    return (current application's NSString's alloc()'s initWithData:outData encoding:(current application's NSUTF8StringEncoding)) as text
end doShellScript

更新履歴

  • 2015-12-16: URLデコードを使う方法で作成
  • 2015-12-18: data部分の取得方法をoffsetからwordに変更
  • 2016-01-10: URLDecodeハンドラを、PHPを使ったdo shell scriptからCocoa機能での変換へと変更
  • 2016-01-11: xxdを使ったdo shell scriptに変更
  • 2016-02-20: シェルコマンドの実行をObjective-Cに変更
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした