--スクリプトエディタの最前面にあるスクリプトをを HTML に変換するスクリプトです。
--スクリプトメニューに入れてご使用ください。そうしないとこのスクリプト自体が変換対象となります。
--このスクリプトをアプリケーションフォーマットで保存した場合、途中でエラーをおこして使えませんでした。
--基本的にブラウザに表示されたコードをコピー・ペーストして、そのままスクリプトエディタで実行することができますが、ペーストすると文字列中に改行コード ¥r(キャラクタコード13) を使用していた場合でも、¥n(キャラクタコード10)に変わります。また、行頭がスペースで始まる場合、そのスペースをタブと認識してしまい消されてしまいますので、¥r が必要な時には return ¥n が必要な時には ASCII character 10 を使用することをお勧めします。(行頭のスペースが消えるのはバグなのでいずれ直そうと思っています)
--このスクリプトは MacOSX10.3.9、AppleScript1.9.3、スクリプトエディタ2.0(v43.1)で動作確認をしました。他のバージョンでは動かない場合があります。
--このスクリプトの使用により何らかの不具合が発生したとしても、作者は責任を負いません。
set tabSize to 24 --タブ一つあたりのピクセルサイズ
set returnLf to ASCII character 10
global tabSize, returnLf
--全体のタグ設定
on setHeader(title, body)
return "<!DOCTYPE html PUBLIC ¥"-//W3C//DTD HTML 4.01 Transitional//EN¥">
<html lang=¥"ja¥">
<head>
<meta http-equiv=¥"content-type¥" content=¥"text/html;charset=Shift_JIS¥">
<title>" & title & "</title>
<style type=¥"text/css¥" media=¥"screen¥"><!--
h2 { font-size: 20px; line-height: 24px; margin-top: 0px; margin-bottom: 0px; }
a { font-size: 12px; line-height: 24px; margin-top: 0px; margin-bottom: 0px; }
p { font-size: 12px }
hr { margin-top: 4px; margin-bottom: 0px }
#center { text-align: center }
--></style>
</head>
<body bgcolor=¥"#ffffff¥" leftmargin=¥"10¥" marginheight=¥"10¥" marginwidth=¥"10¥" topmargin=¥"10¥">
<div id=¥"center¥"><h2><font color=¥"#ffffff¥">--</font>" & title & "<font color=¥"#ffffff¥">--</font></h2></div>
<hr>
<div id=¥"center¥"><a href=¥"http://www.iketch.info/¥">--制作者:イケッチ--</a></div>
<hr>
" & body & "
<hr>
<div id=¥"center¥"><a href=¥"http://www.iketch.info/¥">-- Iketch Design Office --</a></div>
</body>
</html>"
end setHeader
--段落タグの設定
on pTag(countTab, aPara)
return "<p style=¥"margin-bottom: 0pt; margin-left: " & (tabSize * countTab + tabSize div 2 as text) & "pt; margin-top: 0pt; text-indent: " & (-tabSize div 2 as text) & "pt; ¥">" & aPara & "</p>" & return
end pTag
--フォントタグの設定
on fontTag(aColor, aText)
return "<font color=¥"#" & colorTag(aColor) & "¥">" & changeExtraChar(aText) & "</font>"
end fontTag
set fileExists to false
tell application "Script Editor"
if (count document) is not 0 then
set aProp to properties of document 1
if (count aProp) > 7 then
if contents of aProp is not "" then
set everyColor to color of every attribute run of every paragraph of document 1
set everyText to every attribute run of every paragraph of document 1
set thisName to name of document 1
set fileExists to true
else
display dialog "変換する文字がありません" buttons "OK"
end if
else
display dialog "前面にあるのはスクリプトではありません" buttons "OK"
end if
else
display dialog "ドキュメントがありません" buttons "OK"
end if
end tell
if fileExists is true then
if thisName contains "." then
set i to count character of thisName
repeat until i = 1
set i to i - 1
if character i of thisName is "." then exit repeat
end repeat
set title to text 1 thru i of thisName
set thisName to title & "html"
else
set title to thisName
set thisName to thisName & ".html"
end if
set refFile to (choose file name with prompt "保存場所を指定してください" default name thisName) as text
set oldDelim to text item delimiters of AppleScript
set text item delimiters of AppleScript to ":"
set refFolder to ((text items 1 thru -2 of refFile) as text) as alias
set theName to (text item -1 of refFile)
set text item delimiters of AppleScript to oldDelim
set i to 1
set body to ""
repeat count everyColor times
set body to body & setPTag(item i of everyText, item i of everyColor)
set i to i + 1
end repeat
set htmlCode to setHeader(title, body)
tell application "Finder"
if name of every item of refFolder contains theName then
delete refFile as alias
end if
set newFile to make file at refFolder with properties {name:theName}
end tell
set fh to open for access (refFile as alias) with write permission
try
write htmlCode to fh
on error errMsg number errNo
close access fh
error errMsg number errNo
end try
close access fh
beep
end if
--段落の設定
on setPTag(aParaText, aParaColor)
set aPara to ""
set countAPara to count item of aParaColor
set i to 1
set checkVisib to checkNotVisibleChar(item i of aParaText)
set countTab to countTab of checkVisib
if visiblechar of checkVisib then
set aText to text (countTab + 1) thru -1 of item i of aParaText
set aColor to item 1 of aParaColor
set i to i + 1
else if countAPara > 1 then
set i to i + 1
set aText to item i of aParaText
set aColor to item i of aParaColor
set i to i + 1
else
return pTag(0, "<br>")
end if
repeat until countAPara < i
if visiblechar of checkNotVisibleChar(item i of aParaText) then
if aColor is item i of aParaColor then
set aText to aText & item i of aParaText
else
set aPara to aPara & fontTag(aColor, aText)
set aText to item i of aParaText
set aColor to item i of aParaColor
end if
else
set aText to aText & item i of aParaText
end if
set i to i + 1
end repeat
if character -1 of aText is returnLf then set aText to text 1 thru -2 of aText
set aPara to aPara & fontTag(aColor, aText)
return pTag(countTab, aPara)
end setPTag
--文字列中の特殊文字を変換
on changeExtraChar(aText)
set i to 1
set newText to ""
repeat count character of aText times
set aChar to character i of aText
if aChar is "<" then
set newText to newText & "<"
else if aChar is ">" then
set newText to newText & ">"
else if aChar is "¥¥" then
set newText to newText & "¥"
else if aChar is "&" then
set newText to newText & "&"
else if aChar is "¥"" then
set newText to newText & """
else
set newText to newText & aChar
end if
set i to i + 1
end repeat
return newText
end changeExtraChar
--文字列先頭の目に見えない文字をカウント
on checkNotVisibleChar(aText)
set countChar to count character of aText
set i to 1
repeat countChar times
set aChar to character i of aText
if aChar is not tab and aChar is not space and aChar is not returnLf and aChar is not return then
return {visiblechar:true, countTab:i - 1}
end if
set i to i + 1
end repeat
return {visiblechar:false, countTab:i - 1}
end checkNotVisibleChar
--RGBのリストをWebカラーのテキストに変換
on colorTag(RGBList)
set R to hex((item 1 of RGBList) div 256, 2)
set G to hex((item 2 of RGBList) div 256, 2)
set B to hex((item 3 of RGBList) div 256, 2)
return R & G & B
end colorTag
--数値を16進数に変換
on hex(aInt, digit)
set hexNum to ""
repeat digit times
set N to aInt mod 16
if N < 10 then
set N to N as text
else if N = 10 then
set N to "a"
else if N = 11 then
set N to "b"
else if N = 12 then
set N to "c"
else if N = 13 then
set N to "d"
else if N = 14 then
set N to "e"
else
set N to "f"
end if
set hexNum to N & hexNum
set aInt to aInt div 16
end repeat
return hexNum
end hex