AppleScript名:PDFの指定ページを削除 v4(複数ページ一括指定) |
— Modified 2017-08-19 by Takaaki Naganoya –Original By Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" property NSSortDescriptor : a reference to current application’s NSSortDescriptor property NSArray : a reference to current application’s NSArray property NSSet : a reference to current application’s NSSet property |NSURL| : a reference to current application’s |NSURL| property PDFDocument : a reference to current application’s PDFDocument set inFile to (choose file of type {"pdf"} with prompt "Choose your PDF files:") set targPageList to {1, 3, 5, 7, -1, -2} set pRes to removeSpecificPagesFromPDF(inFile, targPageList) of me –指定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 my uniquify1DList(newList, true) set t2List to my sort1DNumList:t1List ascOrder:false return t2List end relativeToAbsNumList on absNum(q) if q is less than 0 then set q to –q return q end absNum –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 sort1DNumList:theList ascOrder:aBool 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 sort1DNumList:ascOrder: |
More from my site
(Visited 38 times, 1 visits today)