2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

2
Last updated at Posted at 2015-12-18
  • 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に変更
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?