- 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に変更