Automator のお題をもう一つ。今度はプリントプラグインです。プリントプラグインとは、印刷ダイアログの左下にある「PDF ▼」で実行するワークフローです。このワークフローは、プリントシステムから PDF ファイルを受け取り、それに対する処理を行うものです。
プリントプラグインは、かつて PDF ワークフローと呼ばれていたようで、Mac OS X 10.6 に付属の「PDF を Web Receipts フォルダに保存」は現在のプリントプラグインとは違う形式で作られています。これをプリントプラグインで作り直してみます。ただ作り直すだけではつまらないので、Web Receipts フォルダに印刷を実行した日の日付のフォルダを作って、その中に PDF ファイルを保存する仕様に変更します。
Automator で、プリントプラグインのテンプレートを使い、「シェルスクリプトを実行」のアクションで、以下の Python スクリプトを実行させます (ポップアップのシェルを /usr/bin/python にする) 。
オリジナルの「PDF を Web Receipts フォルダに保存」の実体は Python で書かれています。これが、同じファイル名で上書きしないなど、色々気を遣ってよくできているので、これをパクって作ります。PDF ワークフローとは異なり、ファイル名が渡されるだけですので、安全なファイル名に変換するルーチンは削除してあります (/tmp に保存されているのだから、既に安全なファイル名のはず) 。適当に手直ししてますが、清野は Python を使ったことがないので、変なことを書いているかもしれません。
#!/usr/bin/python
#
# This script is stolen from
# /Library/PDF Services/Save PDF to Web Receipts Folder.pdfworkflow/Contents/tool
import os
import shutil
import sys
import time
def main(argv):
(title, ext) = os.path.splitext(os.path.basename(argv[0]))
pdfFile = argv[0]
today = time.strftime("%Y-%m-%d", time.localtime())
destDirectory = os.path.expanduser("~/Documents/Web Receipts/" + today + "/")
# Create the Web Receipts folder if necessary.
try:
os.makedirs(destDirectory)
except:
pass
# Build a file path pointing into the web reciepts folder
# using the document's title and a PDF extension.
destFile = title + ".pdf"
destPath = os.path.join(destDirectory, destFile)
# If the filename we want is already in use then start
# appending numbers until we get a unique name.
i = 2
while (os.path.exists(destPath)):
destFile = title + "." + str(i) +".pdf"
destPath = os.path.join(destDirectory, destFile)
i = i + 1
# Move the file if possible otherwise copy it.
shutil.move(pdfFile, destPath)
if __name__ == "__main__":
main(sys.argv[1:])
これを、~/Library/PDF Services/ に保存します。Automator の画面は、以下のようになります。
清野は、Files lite との同期用スクリプトを少しいじって、Web Receipts フォルダの内容を iPod touch へ転送するようにしています。ちょっと気になった Web ページなどを保存しておき、後で iPod touch でゆっくり読むためです。
0 件のコメント:
コメントを投稿