PDFから指定ページ以降を削除するAppleScriptです。
こんな(↑)418ページもあるPDFから、おためし版の、冒頭から40ページだけ抽出したPDFを作成する際に使用したものです。
もともとは418ページありましたが、、、
実行後は40ページに、、、
AppleScript名:PDFから指定ページ以降を削除 |
— Modified 2018-07-08 by Takaaki Naganoya –Original By Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "QuartzCore" use aBplus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property NSSet : a reference to current application’s NSSet property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property SMSForder : a reference to current application’s SMSForder property NSIndexSet : a reference to current application’s NSIndexSet property PDFDocument : a reference to current application’s PDFDocument property NSSortDescriptor : a reference to current application’s NSSortDescriptor load framework set sPage to 41 –ここから末尾までのページを削除 set inFile to (choose file of type {"pdf"} with prompt "Choose your PDF files:") set pRes to removePDFPageAfter(inFile, sPage) of me –指定パスのPDFの指定ページ以降をすべて削除 on removePDFPageAfter(inFile, sPage) set pCount to pdfPageCount(inFile) of me set eCount to pCount – sPage + 1 set aindexSet to NSIndexSet’s indexSetWithIndexesInRange:(current application’s NSMakeRange(sPage, eCount)) set targPageList to (SMSForder’s arrayWithIndexSet:aindexSet) as list return removeSpecificPagesFromPDF(inFile, targPageList) of me end removePDFPageAfter –指定PDF書類の複数ページの一括削除 on removeSpecificPagesFromPDF(inFileAlias, targPageNumList as list) set inNSURL to |NSURL|’s fileURLWithPath:(POSIX path of inFileAlias) set theDoc to PDFDocument’s alloc()’s initWithURL:inNSURL –削除対象ページリストをユニーク化して降順ソート(後方から削除) set pRes to theDoc’s pageCount() set t3List to relativeToAbsNumList(targPageNumList, pRes) of me repeat with i in t3List copy i to targPageNum (theDoc’s removePageAtIndex:(targPageNum – 1)) end repeat –Overwrite Exsiting PDF set aRes to (theDoc’s writeToURL:inNSURL) as boolean return aRes end removeSpecificPagesFromPDF –絶対ページと相対ページが混在した削除対象ページリストを絶対ページに変換して重複削除して降順ソート on relativeToAbsNumList(aList, aMax) set newList to {} repeat with i in aList set j to contents of i if i < 0 then set j to aMax + j end if if (j ≤ aMax) and (j is not equal to 0) then set the end of newList to j end if end repeat set t1List to uniquify1DList(newList, true) of me set t2List to sort1DNumListWithOrder(t1List, false) of me return t2List end relativeToAbsNumList –1D/2D Listをユニーク化 on uniquify1DList(theList as list, aBool as boolean) set aArray to NSArray’s arrayWithArray:theList set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self" return bArray as list end uniquify1DList –Sort 1-Dimension List(String Number List) on sort1DNumListWithOrder(theList as list, aBool as boolean) tell NSSet to set theSet to setWithArray_(theList) tell NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_("floatValue", aBool) set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor} return (sortedList) as list end sort1DNumListWithOrder –指定PDFのページ数をかぞえる(10.9対応。普通にPDFpageから取得) –返り値:PDFファイルのページ数(整数値) on pdfPageCount(aFile) set aFile to POSIX path of aFile set theURL to |NSURL|’s fileURLWithPath:aFile set aPDFdoc to PDFDocument’s alloc()’s initWithURL:theURL set aRes to aPDFdoc’s pageCount() return aRes as integer end pdfPageCount |