on run
errormsg("チェック対象の HTML ファイルをひとつだけドラドロするべし。")
end run
on open thefile
--- 各種設定値
--- Another HTML-lint CGI のURL
set lintURL to "http://openlab.ring.gr.jp/k16/htmllint/htmllint.cgi"
--- 受け付ける拡張子のリスト
set acceptSuffix to {".html", ".htm", ".shtml"}
--- フォームデータとしてそのまま送るとヤバそうな文字と、そのエスケープの対
set escStrPair to {{"%", "%25"}, {";", "%3B"}, {"&", "%26"}, {"+", "%2B"}}
--- 作業テンポラリフォルダ指定
set tmpFolder to (path to temporary items folder) as string
--- 作業テンポラリファイル名
set tmpFileName to "DropValidate_tmp.html"
--- ファイル個数チェック
if number of thefile is not 1 then
errormsg("ドラドロするのは HTML ファイルをひとつだけでヨロシク。")
end if
--- 拡張子チェック
set i to 0
repeat with curItem in acceptSuffix
if (thefile as string) contains curItem then
exit repeat
else
set i to (i + 1)
end if
end repeat
if i = (number of acceptSuffix) then
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\", \""
set suffix to acceptSuffix as string
set AppleScript's text item delimiters to tmp
errormsg("フォルダや、拡張子が \"" & suffix & "\" でない物は受け付けません。")
end if
--- HTML ソース読み出し
try
open for access thefile
set htmlData to read thefile
close access thefile
on error
errormsg("理由はともかく、できませんでした。")
end try
--- フォームデータとしてそのまま送るとヤバそうな文字をエスケープ
repeat with curList in escStrPair
set yabaiMozi to text item 1 of curList
set escMozi to text item 2 of curList
set htmlData to replaceString(htmlData, yabaiMozi, escMozi)
end repeat
--- フォームデータ作成
set formData to "Method=Data&ViewSource=on&Data=" & htmlData
--- Another HTML-lint の CGI へ投げて結果をげっと
tell application "Finder"
set resultFile to tmpFolder & tmpFileName
try
tell application "URL Access Scripting"
with timeout of 300 seconds
download lintURL to file resultFile form data formData replacing yes
end timeout
end tell
--- ふぁいるおーぷん
open file resultFile
on error
errormsg("Another HTML-lint からの結果受け取りに失敗したですよ。")
end try
end tell
end open
--- 文字列 theString 中の findStr を replaceStr に置換
on replaceString(theString, findStr, replaceStr)
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to findStr
set theList to every text item of theString
set AppleScript's text item delimiters to replaceStr
set theString to theList as string
set AppleScript's text item delimiters to tmp
return theString
end replaceString
--- エラーダイアログ表示
on errormsg(msg)
display dialog msg
error number -128 --- 強制終了
end errormsg