2013年5月22日水曜日

自炊した小説を読みやすくするアプリを作りました

随分前に書いた、この記事の総集編です。

ImageMagickを使って自炊文庫本を読みやすくする | How2すいとー
ImageMagikとかxpdfとか



自炊文庫本をいい感じにするってやつなんですが、使いやすいようにアプリにしました。
本当はTerminalで完結させたかったけど、よーわからんってなってAppleScript製です。
前記事のとおり、ImageMagikとxpdfがインストールされている必要があります。

文庫本製本.app

使いかた



実行すると、PDFの選択ダイアログが出ます。複数選択できます。
最初の数ページは表紙とかでカラーなことが多いので、そのページ数を入力。
処理的には、グレースケールにしないページを指定してます。

全冊に同じカラーページ数が使われます。
また、途中にあるカラーは無視してます。
不親切だけど、細かく設定しすぎても使いづらいかなと思ってこんな仕様です。
困る場合はスクリプトを編集してください。

ちなみにdpiは300で書き出すので、変えたい場合はここも編集対象。

あとはほったらかしでOKです。
終わったらデスクトップに吐き出されます。

かなり時間がかかります。
dpiを小さくすればそれなりに速くはなりますが、その間Finderがやたら重くなります。
動作は寝てる間推奨ですね。

また、動作中はHDDの容量をそれなりに食います。
これもdpiとページ数次第ですが、3GBくらいは余裕がほしいです。

とまあこんな感じです。
なんだかワガママ仕様ですが、一度設定すればまだ使いやすい部類かなと思います。
コードもシンプルでアレンジしやすいので、自炊してる人は使いまわしてください。

文庫本製本.app

中のコードはこちら。

--PDFの自炊文庫本を製本
tell application "Finder"
set tempFolName to "bookbindtemp" --作業フォルダ名(被るようなら変更)
set tempFol to (path to desktop) & tempFolName as text
set pdfFiles to choose file with prompt "PDFファイルを選択" default location (path to home folder) with multiple selections allowed
try
make new folder at (path to desktop) with properties {name:tempFolName}
on error
set ANS to display dialog "フォルダが重複しています。上書きしますか?"
delete tempFol
make new folder at (path to desktop) with properties {name:tempFolName}
end try
duplicate pdfFiles to tempFol with replacing
set pdfFiles to every file of (tempFol as alias)
end tell

set ANS to display dialog "カラーページ数を入力
複数選択の場合はすべてに適用" default answer "4"
set colorNum to text returned of ANS as number
set dpi to "400" --書き出すdpi値、変更可

set i to 1
repeat with pdfFIle in pdfFiles
with timeout of 3600 seconds
tell application "Finder"
set pdfFileName to name of pdfFIle
set tempFolAlias to tempFol as alias
make new folder at tempFolAlias with properties {name:i}
set tempFolAliasi to tempFolAlias & i as text
move pdfFIle to (tempFolAliasi as alias)

set CMD1 to "/opt/local/bin/xpdf-pdftoppm -r " & dpi & " '" & (tempFolAliasi)'s POSIX path & "/" & pdfFileName & "' '" & (tempFolAliasi)'s POSIX path & "/temp'"
do shell script CMD1

set CMD2 to "/opt/local/bin/mogrify -format jpg '" & (tempFolAliasi)'s POSIX path & "/temp*.ppm'"
do shell script CMD2

tell folder tempFolAliasi
set ppmFiles to (every file whose name ends with ".ppm")
end tell
delete ppmFiles

tell folder tempFolAliasi
set jpgFiles to (every file whose name ends with ".jpg")
end tell
set jpgFilesColor to items 1 thru colorNum of jpgFiles
repeat with a in jpgFilesColor
set name of a to name of a & "1"
end repeat

set CMD3 to "/opt/local/bin/mogrify -level 0%,75%,0.8 -type grayscale -deskew 40% '" & (tempFolAliasi)'s POSIX path & "/temp*.jpg'"
do shell script CMD3

tell folder tempFolAliasi
set jpg1Files to (every file whose name ends with ".jpg1")
end tell
repeat with a in jpg1Files
set b to name of a as text
set b to reverse of (rest of (reverse of characters of b)) as text --末尾文字削除
set name of a to b
end repeat

set CMD4 to "/opt/local/bin/convert '" & (tempFolAliasi)'s POSIX path & "/temp*.jpg' '" & (path to desktop)'s POSIX path & "/" & pdfFileName & "'"
do shell script CMD4

delete tempFolAliasi
set i to i + 1

end tell
end timeout
end repeat

tell application "Finder" to delete tempFol

0 件のコメント:

コメントを投稿