AppleScript名:指定フォルダ内のPDFの1ページ目をすべて別のPDFにまとめる |
— Created 2017-09-12 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "QuartzCore" property |NSURL| : a reference to current application’s |NSURL| property NSPredicate : a reference to current application’s NSPredicate property NSFileManager : a reference to current application’s NSFileManager property PDFDocument : a reference to current application’s PDFDocument property NSMutableArray : a reference to current application’s NSMutableArray property NSURLIsDirectoryKey : a reference to current application’s NSURLIsDirectoryKey property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles property NSDirectoryEnumerationSkipsPackageDescendants : a reference to current application’s NSDirectoryEnumerationSkipsPackageDescendants property NSDirectoryEnumerationSkipsSubdirectoryDescendants : a reference to current application’s NSDirectoryEnumerationSkipsSubdirectoryDescendants set aFol to POSIX path of (choose folder with prompt "Choose folder" default location (path to pictures folder)) set targPDF to POSIX path of (choose file name with prompt "Choose File Name" default location (path to desktop folder) default name ((do shell script "uuidgen") & ".pdf")) set pRes to gatherEachFirstPage(aFol, targPDF) of me on gatherEachFirstPage(aFol, targPDF) set aURL to (|NSURL|’s fileURLWithPath:targPDF) set outPDFdoc to PDFDocument’s alloc()’s init() set aList to my getFilesByExtension:{"pdf"} fromDirectory:(aFol) set outPDFPageCount to 0 repeat with i in aList –集めたPDFの1ページ目を取得 set bURL to (|NSURL|’s fileURLWithPath:(POSIX path of i)) set bPDFdoc to (PDFDocument’s alloc()’s initWithURL:bURL) set bPage to (bPDFdoc’s pageAtIndex:0) –first page –まとめ先のPDFに追記 (outPDFdoc’s insertPage:bPage atIndex:outPDFPageCount) set outPDFPageCount to outPDFPageCount + 1 end repeat return (outPDFdoc’s writeToURL:aURL) as boolean end gatherEachFirstPage –指定フォルダ内の指定拡張子のファイルを抽出する on getFilesByExtension:listOfExtensions fromDirectory:sourceFolder set fileManager to NSFileManager’s defaultManager() set aURL to |NSURL|’s fileURLWithPath:sourceFolder set theOptions to ((NSDirectoryEnumerationSkipsPackageDescendants) as integer) + ((NSDirectoryEnumerationSkipsHiddenFiles) as integer) set directoryContents to fileManager’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value) set foundItemList to NSPredicate’s predicateWithFormat_("pathExtension.lowercaseString IN %@", listOfExtensions) set foundItemList to directoryContents’s filteredArrayUsingPredicate:foundItemList # Return as a list POSIX Paths set foundItemList to (foundItemList’s valueForKey:"path") as list return foundItemList end getFilesByExtension:fromDirectory: |
More from my site
(Visited 35 times, 1 visits today)