PDF分割
20年前からPDFファイル分割のアップルスクリプトを使っていますが、OSをEl Capitanにしてから動かなくなったので修正
少し動いては止まるので原因を考えたところ、どうもAcrobatの処理速度が間に合っていない様子。たしか時間調整のコマンドがあったはずと調べて修正したところ問題が解決した。
delay 0.3
の1行を追加しただけだけれど‥‥
参考にスクリプトファイルを公開します
PDFファイルをしおり毎にファイルを分割するスクリプトです
日本語のしおり名は文字化けするので使わないこと
また、同じ名前のしおり名をつけると止まります。エラー処理も何もしていないので‥‥
tell application "Finder"
set itemList to (selection as list)
set kosu to count itemList
if kosu > 0 then
repeat with TargetFile in itemList
set TargetFile to TargetFile as alias
--set TargetFile to TargetFile as string
--set TargetFile to path of TargetFile
--set TargetFile to characters 2 thru end of TargetFile
--set TargetFile to TargetFile as string
--set myPath to ":" as alias
set PDFtoolsPath to (path to home folder)
set PDFtoolsPath to PDFtoolsPath & "WhitePaper.pdf"
set PDFtoolsPath to PDFtoolsPath as string
--display dialog PDFtoolsPath
--set TargetFile to (myPath as string) & TargetFile
set as_BookmarkPageList to {}
set as_BookmarkNameList to {}
tell application "Adobe Acrobat Professional"
--display dialog TargetFile
open file TargetFile
activate
set as_docName to name of document 1
set as_docFileName to file alias of document 1
set bkmks to bookmarks of document 1
display dialog "open"
set doc to document 1
set bkmk to bookmarks of doc
set n to count each bkmk
set indicatorNum to 1
repeat with nn from 1 to n
set as_BookmarkPageList to as_BookmarkPageList & destination page number of (bookmark nn)
set as_BookmarkNameList to as_BookmarkNameList & name of bookmark nn
set indicatorNum to indicatorNum + 1
end repeat
set as_BookmarkPageList to as_BookmarkPageList & (count (pages of document 1)) + 1
--save document 1
end tell
repeat with nn from 1 to n
set as_ID to item nn of as_BookmarkPageList
set as_IDName to item nn of as_BookmarkNameList
tell application "Finder"
set as_SavePath to container of file TargetFile
set as_whitePaperFile to PDFtoolsPath as alias
duplicate as_whitePaperFile to as_SavePath with replacing
set as_whitePaperFile2 to result
open as_whitePaperFile2
--display dialog as_whitePaperFile2 as string
set as_docNameWhite to name of as_whitePaperFile2
tell application "Adobe Acrobat Professional"
set as_1stPage to as_ID
set as_lastPage to item (nn + 1) of as_BookmarkPageList
set as_totalPage to as_lastPage - as_1stPage
insert pages document as_docNameWhite from document as_docName starting with as_1stPage number of pages as_totalPage after 0 without insert bookmarks
set newPath to as_whitePaperFile2 as string
set ps to pages of document as_docNameWhite
set pNum to kisuGusuNum + (count ps)
if pNum mod 2 = 1 then
delete last page of document as_docNameWhite
end if
save document as_docNameWhite to file newPath
close document as_docNameWhite
end tell
set as_docName2 to (as_docName as text)
set as_docName2 to characters 1 thru -5 of as_docName2
set as_docName2 to (as_docName2 as string)
set as_saveFile to as_docName2 & as_IDName & ".pdf"
set name of as_whitePaperFile2 to as_saveFile as string
end tell
end repeat
tell application "Adobe Acrobat Professional"
close document 1
end tell
end repeat
end if
end tell
on open itemList
set flg to true
set indicatorNum to 0
set kisuGusuNum to 0
repeat with TargetFile in itemList
--set TargetFile to TargetFile as string
--set TargetFile to path of TargetFile
--set TargetFile to characters 2 thru end of TargetFile
--set TargetFile to TargetFile as string
--set myPath to ":" as alias
set PDFtoolsPath to (path to home folder)
set PDFtoolsPath to PDFtoolsPath & "WhitePaper.pdf"
set PDFtoolsPath to PDFtoolsPath as string
--display dialog PDFtoolsPath
--set TargetFile to (myPath as string) & TargetFile
set as_BookmarkPageList to {}
set as_BookmarkNameList to {}
tell application "Adobe Acrobat Professional"
--display dialog TargetFile
open file TargetFile
activate
set as_docName to name of document 1
set as_docFileName to file alias of document 1
set bkmks to bookmarks of document 1
set doc to document 1
set bkmk to bookmarks of doc
set n to count each bkmk
set indicatorNum to 1
repeat with nn from 1 to n
set as_BookmarkPageList to as_BookmarkPageList & destination page number of (bookmark nn)
set as_BookmarkNameList to as_BookmarkNameList & name of bookmark nn
set indicatorNum to indicatorNum + 1
end repeat
set as_BookmarkPageList to as_BookmarkPageList & (count (pages of document 1)) + 1
--save document 1
end tell
repeat with nn from 1 to n
set as_ID to item nn of as_BookmarkPageList
set as_IDName to item nn of as_BookmarkNameList
tell application "Finder"
set as_SavePath to container of file TargetFile
set as_whitePaperFile to PDFtoolsPath as alias
duplicate as_whitePaperFile to as_SavePath with replacing
set as_whitePaperFile2 to result
open as_whitePaperFile2
--display dialog as_whitePaperFile2 as string
delay 0.15
set as_docNameWhite to name of as_whitePaperFile2
tell application "Adobe Acrobat Professional"
set as_1stPage to as_ID
set as_lastPage to item (nn + 1) of as_BookmarkPageList
set as_totalPage to as_lastPage - as_1stPage
insert pages document as_docNameWhite from document as_docName starting with as_1stPage number of pages as_totalPage after 0 without insert bookmarks
set newPath to as_whitePaperFile2 as string
set ps to pages of document as_docNameWhite
set pNum to kisuGusuNum + (count ps)
if pNum mod 2 = 1 then
delete last page of document as_docNameWhite
end if
save document as_docNameWhite to file newPath
close document as_docNameWhite
end tell
set as_docName2 to (as_docName as text)
set as_docName2 to characters 1 thru -5 of as_docName2
set as_docName2 to (as_docName2 as string)
set as_saveFile to as_docName2 & as_IDName & ".pdf"
set name of as_whitePaperFile2 to as_saveFile as string
end tell
end repeat
tell application "Adobe Acrobat Professional"
close document 1
end tell
end repeat
tell application "Finder" to beep
return flg
end open