2009-10-08
■[PDF][Acrobat][AppleScript]PDFのページ数の得る
Acrobat 9 ProのAppleScriptでPDFドキュメントの総ページ数が簡単に得られる。あれ? 昔からそうでしたっけ? pdfinfoとか使っていたのは何故?
set aFile to choose file tell application "Adobe Acrobat Pro" open aFile tell document 1 set my_count to count pages display dialog my_count as Unicode text close saving no end tell end tell
複数のPDFをドラッグ&ドロップで処理したいならこんな感じ。
(* count_PDF_pages PDFドキュメントをドラッグ&ドロップして、PDFの総ページ数をカウントします。 -Mac OS X 10.6.1 -Acrobat 9 Pro 2009-10-08 *) --------------------------------------------------------●ドラッグ&ドロップされた on open of theFiles set my_count to 0 --全体のカウンタ set my_files to my file_kind({"pdf"}, {"PDF "}, theFiles) --ファイルのフィルタリング tell application "Adobe Acrobat Pro" try repeat with aFile in my_files open aFile tell document 1 set my_count to my_count + (count pages) as integer close saving no end tell end repeat on error errMsg number errNum display dialog ((name of aFile) as Unicode text) & return & "Error! This file failure." & return & errMsg buttons {"Cancel"} end try end tell display dialog my_count as Unicode text end open ----------------------------------------------●必要なファイルだけをフィルタして返します to file_kind(extention_list, type_list, theFiles) set my_files to {} ignoring case tell application "Finder" repeat with i in theFiles if extention_list contains ((name extension of i) as Unicode text) then set end of my_files to contents of i else if (kind of i) is "フォルダ" as Unicode text then set my_files to my_files & my file_kind(extention_list, type_list, every file in folder i) else if type_list contains ((file type of i) as Unicode text) then set end of my_files to contents of i end if end repeat end tell end ignoring return my_files end file_kind
しかしながら、いちいち開くんだからそれなりに遅いです。Xpdfのpdfinfoコマンドが使える環境ならこんな感じに書けば早いです。やっぱりpdfinfoだ^^
(追記:2010-01-21T00:35:55+0900)ちょっと修正しました
(* count_PDF_pages_pdfinfo PDFドキュメントをドラッグ&ドロップして、PDFの総ページ数をカウントします。 -Mac OS X 10.6.1 -pdfinfo 3.02 2009-10-08 2010-01-21 フォルダをドラッグ&ドロップしたときに失敗するのを修正 *) --------------------------------------------------------●ドラッグ&ドロップされた on open of theFiles set my_count to 0 --全体のカウンタ set my_files to my file_kind({"pdf"}, {"PDF "}, theFiles) --ファイルのフィルタリング try repeat with aFile in my_files set aFile to quoted form of POSIX path of (contents of aFile) set ans to do shell script ("/usr/local/bin/pdfinfo " & aFile & "| grep ^Pages: | sed -e 's/^Pages: *//;'")--インストールパスは環境 --set ans to do shell script ("/Applications/pdftk.app/bin/pdfinfo " & aFile & "| grep ^Pages: | sed -e 's/^Pages: *//;'") set my_count to my_count + (ans) as integer end repeat on error errMsg number errNum display dialog ((name of aFile) as Unicode text) & return & "Error! This file failure." & return & errMsg buttons {"Cancel"} end try display dialog my_count as Unicode text end open ----------------------------------------------●必要なファイルだけをフィルタして返します to file_kind(extention_list, type_list, theFiles) set my_files to {} ignoring case tell application "Finder" repeat with i in theFiles if extention_list contains ((name extension of i) as Unicode text) then set end of my_files to (contents of i) as alias else if (kind of i) is "フォルダ" as Unicode text then set my_files to my_files & my file_kind(extention_list, type_list, every file in folder i) else if type_list contains ((file type of i) as Unicode text) then set end of my_files to (contents of i) as alias end if end repeat end tell end ignoring return my_files end file_kind
Mac環境でない場合は、PDFをゴリゴリ読んで調べる方法もある。
Adobe Forums: How can I get a total page count of a...
(追記:2009-10-08T13:43:02+0900)komさんのコメントにありますとおり、mdlsコマンドも使用できるようです(ただしspotlightに捕捉されていれば)。mdlsコマンドについては下記を参照。komさん、ありがとうございました。
参照
インストール メモ - xpdf 3.01(ちょっと古いです。注意)
AS Hole(AppleScriptの穴) By Piyomaru Software » pdfinfoを使ってPDFのページ数をかぞえる » Blog Archive
AS Hole(AppleScriptの穴) By Piyomaru Software » pdfinfoの結果をparseするv2 » Blog Archive
トラックバック - http://d.hatena.ne.jp/seuzo/20091008/1254930176
リンク元
- 81 http://www.google.co.jp/search?q=PDF+ページ数&btnG=検索&hl=ja&lr=lang_ja&client=firefox-a&rls=org.mozilla:ja:official&hs=Jfw&sa=2
- 71 http://search.yahoo.co.jp/search?p=PDF+ページ数+script&ei=UTF-8&fr=top_ga1_sa&x=wrt
- 43 http://www.seuzo.jp/st/scripts_other/index.html
- 34 http://www.seuzo.jp/st/index.html
- 29 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GPTB_jaJP292JP293&q=PDFファイル 総ページ数
- 22 http://www.google.co.jp/url?sa=t&rct=j&q=pdf ページ数&source=web&cd=17&ved=0CFkQFjAGOAo&url=http://d.hatena.ne.jp/seuzo/20091008/1254930176&ei=9ZGmTuPdB6jhmAWdx-HQDw&usg=AFQjCNEtDypZowhcONshkCSOJbeQNSIYTA&sig2=PUP
- 20 http://www.google.co.jp/search?hl=ja&source=hp&q=pdf+ページ数+カウント&lr=&aq=2&oq=PDF+ページ数
- 17 http://www.google.co.jp/search?hl=ja&q=pdf+ページ数+カウント&btnG=検索&lr=&aq=2&oq=pdf+ページ数+
- 16 http://www.google.co.jp/search?q=PDF ページ数 スクリプト&hl=ja&start=10&sa=N
- 13 http://www.advance-1st.co.jp/cybozu/ag.cgi?page=AGIndex


