何度もすみません。再修正版です。今回も「シェルスクリプトを実行」のみの差し替えで構いません。問題点の修正に加え、挙動も若干変更し、すべての TIFF ファイルを変換するようにしました。
また、「シェルスクリプトを実行」で fmt = 'jpeg' のところを fmt = 'png' と書き換えると、TIFF ファイルを PNG ファイルに変換するようになります。なお、pdf の指定は不可です。
AppleScript を実行
property msg : "RTFD ファイルを選択してください" on run {argv} if (count argv) < 1 then set uti to "com.apple.rtfd" set loc to path to desktop set argv to choose file of type uti with prompt msg ¬ default location loc with multiple selections allowed end if return argv end run
シェルスクリプトを実行
/bin/bash 引数として
python <<'EOF' - "$@" # -*- coding: utf-8 -*- import sys, os from datetime import datetime from subprocess import Popen, PIPE, check_call def cp(opt, src, dst): cmd = ['cp', opt, src, dst] check_call(cmd) def sips(fmt, src, dst): cmd = ['sips', '-s', 'format', fmt, '--out', dst, src] proc = Popen(cmd, stdin = PIPE, stdout = PIPE, stderr = PIPE) out, err = proc.communicate() if err: raise Exception(err) def main(fmt, new_ext): if len(sys.argv) < 2: quit() now = datetime.now().strftime('%Y%m%d%H%M%S') for rtfd in [os.path.abspath(x) for x in sys.argv[1:]]: if not os.path.isdir(rtfd): continue rtfd_dname, rtfd_bname = os.path.split(rtfd) rtfd_fname, rtfd_ext = os.path.splitext(rtfd_bname) if rtfd_ext != ".rtfd": continue rtfd_backup = os.path.join(rtfd_dname, rtfd_fname + '~bk_' + now + rtfd_ext) cp('-pR', rtfd, rtfd_backup) os.chdir(rtfd) for f in os.listdir(rtfd): fname, ext = os.path.splitext(f) n = 1 if ext != '.tiff': continue if not os.path.isfile(fname + new_ext): new_f = fname + new_ext else: new_f = fname + ' ' + str(n) + new_ext while os.path.isfile(new_f): if n > 1000: raise Exception("filename: overflow") n += 1 new_f = fname + ' ' + str(n) + new_ext sips(fmt, f, new_f) os.remove(f) fh = open('TXT.rtf', 'r+') text = fh.read() text = text.replace('\\NeXTGraphic ' + f, '\\NeXTGraphic ' + new_f) fh.seek(0) fh.write(text) fh.truncate() fh.close() print rtfd if __name__ == '__main__': dct = {'jpeg' : '.jpg' , 'png' : '.png' , 'psd' : '.psd'} # # jpeg | png | psd # fmt = 'jpeg' main(fmt, dct[fmt]) EOF
AppleScript を実行
property msg1 : "処理が完了しました。" property msg2 : "項目はありませんでした。" property msg3 : "他 “" property msg4 : "項目”" on run {argv} try set argc to count argv if argv is {""} then set msg to msg2 else if argc < 11 then set msg to join(10, argv) else set msg to join(10, items 1 thru 10 of argv) & return & ¬ msg3 & (count of items 11 thru end of argv) & msg4 end if using terms from application "Finder" display alert msg1 message msg end using terms from on error err_msg number err_num display alert "Error!" message err_num & ": " & err_msg as warning end try end run on join(n, ary) # # tab:9 cr:13 nl:10 space:32 period:46 comma:44 slash:47 # set ifs_org to text item delimiters of AppleScript set text item delimiters of AppleScript to character id (n) set str to ary as text set text item delimiters of AppleScript to ifs_org return str end join
ーーーーー
さて、RTFD 内にある「TXT.rtf」では、異なるテキストエンコーディングが混在する場合があり?、スクリプトで編集するのはちょっとマズいかもしれません。手元のファイルで試した限りでは大丈夫のようですが、ちょっと心配です。
その点では、chandana さんがお書きのように screencapture コマンドでスクリーンショットを PDF 形式でクリップボードにコピーする方法が安全です。
ということで、こんな感じの AppleScript (スクリプト形式) にして、OS のメニューバーから実行するのも良いでしょう。
property fmt : "pdf" on run tell application id get_frontmost_application() to activate do shell script "screencapture -ic -t " & fmt end run on get_frontmost_application() tell application "System Events" get processes whose frontmost is true and visible is true return bundle identifier of item 1 of result end tell end get_frontmost_application
※ 動作確認は Mac OS X 10.6.8 です。