-- アプレット形式で保存して動作させないとダメ。厳守。
-- URL リストファイル (list.txt) はアプレットと同じ所に置くべし (改行コードCR)
-- スクリーンショット画像と HTML はアプレットと同じ場所のフォルダ (result) に保存される
-- 別途 GraphicConverter (http://www.graphicconverter.net) または
-- GraphicsImporter OSAX (http://www.azug.gr.jp/~h-abe/freeware/gio/) が必要。
property URLListFile : "list.txt" -- URL リストのファイル名
property saveFolderName : "result" -- 保存用フォルダの名前
property OutputHTMLFile : "index.html" -- 結果出力 HTML のファイル名
property saveImgFormat : "jpg" -- サムネール画像の画像形式 ("png" / "jpg")
property indexColor : true -- PNG で保存の場合に 256 インデックスカラーにするかどうか (true / false)
property imgQuarity : 95 -- JPEG で保存の場合の保存画質 (1 〜 100)
property captureDelay : 10 -- ページロード開始からスクリーンショットを撮るまでの遅延(秒)
property targetWidth : 800 -- スクリーンショットを撮る際のサイズ(タテ)
property targetHeight : 600 -- スクリーンショットを撮る際のサイズ(ヨコ)
property ResizeWidth : 160 -- スクリーンショットをこのヨコ幅へ縮小
property ResizeHeight : 120 -- スクリーンショットをこのタテ幅へ縮小
property MenuBarHeight : 22 -- MacOSX メニューバーの高さ(変更不要)
property TitleBarHeight : 23 -- MacOSX ウィンドウタイトルバーの高さ(変更不要)
property ScrollBarWidth : 15 -- MacOSX スクロールバーの高さ(変更不要)
global saveFolder, URLList
init()
capture()
outputHTML()
postProcess()
on init()
tell application "Finder"
activate
display dialog " ※ Webnail AppleScript 注意事項 ※" & return & return & ツ
"Camino は JavaScript をオン、ポップアップブロックをオフに。" & ツ
"ディスプレイのスリープは「しない」に。" & ツ
"スクリーンセーバは「起動しない」に、それぞれ設定してください。" & return & return & ツ
"処理が完了するまで、他の作業は事実上不可能になるです。" buttons {"Cancel", "Start"} ツ
default button 2 with icon caution giving up after 60
if gave up of result is true then error number -128
-- 各種作業フォルダを設定
set baseFolder to (container of (path to me)) as alias
set saveFolder to my makeNewFolder(baseFolder, saveFolderName)
-- URL リストの読み出し
set URLList to my readFile(file URLListFile of baseFolder as alias)
end tell
end init
on capture()
set num to 1
repeat with curItem in URLList
tell application "Camino"
-- 新規窓にページを開く
activate
close every window
open location "javascript:window.open('" & uri of curItem & "','','status=no');vold(0);"
close window 1 -- window.open() を走らせただけのウインドウを閉じる
set bounds of window 1 to {0, 0, targetWidth + ScrollBarWidth, targetHeight + TitleBarHeight + ScrollBarWidth}
-- 指定秒待ったらスクリーンショットを撮ってクリップボードへ格納
display dialog "Webnail AppleScript" & return & return & ツ
"進行状況:" & num & "/" & number of URLList ツ
buttons {"中断"} with icon note giving up after captureDelay
if button returned of result is "中断" then error number -128
activate
do shell script "/usr/sbin/screencapture -c -x"
-- ページタイトルを格納してウィンドウをぜんぶ閉じる
set title of curItem to name of window 1
if name of window 1 is "untitled" then set title of curItem to uri of curItem
close every window
set numWins to count of windows -- 後述
end tell
set fPath to (saveFolder as text) & formatNumber(num) & "." & saveImgFormat
-- クリップボードから画像を読んでトリミング、リサイズ、ファイルヘ保存。
SaveImageByGC(fPath) -- GraphicConverter を利用 (処理は遅いが縮小画質や画像圧縮率は良い)
-- SaveImageByGI(fPath) -- GraphicsImporter OSAX を利用 (処理は速いが縮小画質がイマイチ)
-- ドメイン名解決タイムアウトでダイアログが出てる等、何らかの理由でウィンドウクローズに
-- 失敗してる場合、処理を継続できなくなる。その場合は仕方ないので Camino を強制終了
if numWins is not 0 then do shell script "kill `ps -x | grep '[C]amino.app' | cut -b -5`"
set num to num + 1
end repeat
end capture
on outputHTML()
set num to 1
set HTMLFile to open for access ((saveFolder as text) & OutputHTMLFile) with write permission
set eof HTMLFile to 0
write ツ
"" & return & ツ
"" & return & ツ
"" & return & ツ
"
" & return & ツ
"" & return & ツ
"" & return & ツ
"" & return & ツ
"Webnail AppleScript : result" & return & ツ
"" & return & ツ
"" & return & return & ツ
"" & return & return & ツ
"" & ツ
return to HTMLFile
repeat with curItem in URLList
set uri to convToEntityRef(uri of curItem)
set title to convToEntityRef(title of curItem)
write ツ
"- " & ツ
"
" & ツ
" " & return ツ
to HTMLFile
set num to num + 1
end repeat
write ツ
"
" & return & return & ツ
"" & return & ツ
"" & return ツ
to HTMLFile
close access HTMLFile
tell application "Finder"
set file type of file ((saveFolder as text) & OutputHTMLFile) to "TEXT"
set creator type of file ((saveFolder as text) & OutputHTMLFile) to "CHIM"
end tell
end outputHTML
on postProcess()
tell application "Camino" to close every window
tell application "Finder" to open saveFolder
end postProcess
on readFile(thefile)
set theList to {}
try
open for access thefile
repeat
set readline to (read thefile before return)
if readline does not start with "#" and readline is not "" then
set end of theList to {uri:readline, title:""}
end if
end repeat
end try
close access thefile
return theList
end readFile
on makeNewFolder(targetFolder, newFolder)
tell application "Finder"
set cnt to 0
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set existFolders to every item of (targetFolder as alias) whose (name starts with newFolder and kind is "フォルダ")
repeat with curItem in existFolders
set fName to name of curItem
try
set suffix to (text item 2 of fName) as number
if fName starts with (newFolder & ".") and suffix > cnt then set cnt to suffix
end try
end repeat
set AppleScript's text item delimiters to tmp
if (count of existFolders) is not 0 then set newFolder to newFolder & "." & (cnt + 1)
make new folder at (targetFolder as alias) with properties {name:newFolder}
return (folder newFolder of targetFolder) as alias
end tell
end makeNewFolder
on formatNumber(num)
return text ((count of (number of URLList as text)) * -1) thru -1 of ("00000000" & num)
end formatNumber
on convToEntityRef(str)
set pairs to {{"&", "&"}, {"<", "<"}, {">", ">"}, {"\"", """}}
set tmp to AppleScript's text item delimiters
repeat with curItem in pairs
set AppleScript's text item delimiters to (text item 1 of curItem) as Unicode text
set str to every text item of str
set AppleScript's text item delimiters to (text item 2 of curItem) as Unicode text
set str to str as text
end repeat
set AppleScript's text item delimiters to tmp
return str
end convToEntityRef
on SaveImageByGC(fPath)
tell application "System Events" to set visible of process "GraphicConverter" to false
tell application "GraphicConverter"
new image from clipboard
set imgSize to image dimension of window 1
set width to item 1 of imgSize
set height to item 2 of imgSize
set offsetY to MenuBarHeight + TitleBarHeight
change margins window 1 with {0, -1 * offsetY, targetWidth - width, targetHeight - height + offsetY}
scale window 1 horizontal (ResizeWidth / targetWidth) vertical (ResizeHeight / targetHeight) with high quality
if saveImgFormat is "png" then
if indexColor is true then set color space of window 1 to indexed
save window 1 in fPath as PNG with wwwready
else
set JPEG quality of window 1 to imgQuarity
set JPEG progressive of window 1 to true
save window 1 in fPath as JPEG with wwwready
end if
close window 1
end tell
end SaveImageByGC
on SaveImageByGI(fPath)
set offsetY to MenuBarHeight + TitleBarHeight
set pic to the clipboard as picture
giconvert result cropping {0, offsetY, targetWidth, offsetY + targetHeight}
giconvert result bounds {0, 0, ResizeWidth, ResizeHeight}
if saveImgFormat is "png" then
if indexColor is true then
giconvert result type "PNGf" image file fPath depth 8
else
giconvert result type "PNGf" image file fPath
end if
else
giconvert result type "JPEG" image file fPath quality imgQuarity
end if
end SaveImageByGI