Finder上で選択中のファイルのうちPDFだけを作成日付で古い順に連結するAppleScriptです。
間違ってPDF以外のファイルを選択してしまった場合でも、それについては無視します。
こんな風にmacOS標準装備のScript Menuに入れて利用しています。
AppleScript名:Finderで選択中のPDFを古い順に連結する v2 |
— Created 2018-05-26 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSPredicate : a reference to current application’s NSPredicate property NSFileManager : a reference to current application’s NSFileManager property NSURLPathKey : a reference to current application’s NSURLPathKey property NSMutableArray : a reference to current application’s NSMutableArray property NSSortDescriptor : a reference to current application’s NSSortDescriptor property NSURLIsPackageKey : a reference to current application’s NSURLIsPackageKey property NSURLIsDirectoryKey : a reference to current application’s NSURLIsDirectoryKey property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey property NSURLContentModificationDateKey : a reference to current application’s NSURLContentModificationDateKey property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles –set inFiles to (choose file of type {"pdf"} with prompt "Choose your PDF files:" with multiple selections allowed) tell application "Finder" set inFiles to selection as alias list end tell if inFiles = {} then return –指定のAlias listのうちPDFのみ抽出 set filRes1 to filterAliasListByUTI(inFiles, "com.adobe.pdf") of me –選択中のファイルのうちの1つから親フォルダを求め、出力先ファイルパスを組み立てる set outPathTarg to POSIX path of (first item of filRes1) set pathString to current application’s NSString’s stringWithString:outPathTarg set newPath to (pathString’s stringByDeletingLastPathComponent()) as string set destPosixPath to newPath & "/" & ((current application’s NSUUID’s UUID()’s UUIDString()) as string) & ".pdf" combinePDFsAndSaveIt(filRes1, destPosixPath) of me on combinePDFsAndSaveIt(inFiles, destPosixPath) set inFilesSorted to my filesInListSortFromOldToNew(inFiles) — make URL of the first PDF set inNSURL to current application’s |NSURL|’s fileURLWithPath:(POSIX path of item 1 of inFilesSorted) set theDoc to current application’s PDFDocument’s alloc()’s initWithURL:inNSURL — loop through the rest set oldDocCount to theDoc’s pageCount() set inFilesSorted to rest of inFilesSorted repeat with aFile in inFilesSorted set inNSURL to (current application’s |NSURL|’s fileURLWithPath:(POSIX path of aFile)) set newDoc to (current application’s PDFDocument’s alloc()’s initWithURL:inNSURL) set newDocCount to newDoc’s pageCount() repeat with i from 1 to newDocCount set thePDFPage to (newDoc’s pageAtIndex:(i – 1)) — zero-based indexes (theDoc’s insertPage:thePDFPage atIndex:oldDocCount) set oldDocCount to oldDocCount + 1 end repeat end repeat set outNSURL to current application’s |NSURL|’s fileURLWithPath:destPosixPath (theDoc’s writeToURL:outNSURL) end combinePDFsAndSaveIt on filesInListSortFromOldToNew(aliasList) set keysToRequest to {NSURLPathKey, NSURLIsPackageKey, NSURLIsDirectoryKey, NSURLContentModificationDateKey} set valuesNSArray to NSMutableArray’s array() repeat with i in aliasList set oneNSURL to (|NSURL|’s fileURLWithPath:(POSIX path of i)) (valuesNSArray’s addObject:(oneNSURL’s resourceValuesForKeys:keysToRequest |error|:(missing value))) end repeat set theNSPredicate to NSPredicate’s predicateWithFormat_("%K == NO OR %K == YES", NSURLIsDirectoryKey, NSURLIsPackageKey) set valuesNSArray to valuesNSArray’s filteredArrayUsingPredicate:theNSPredicate set theDescriptor to NSSortDescriptor’s sortDescriptorWithKey:(NSURLContentModificationDateKey) ascending:true set theSortedNSArray to valuesNSArray’s sortedArrayUsingDescriptors:{theDescriptor} — extract just the paths and convert to an AppleScript list return (theSortedNSArray’s valueForKey:(NSURLPathKey)) as list end filesInListSortFromOldToNew –Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す on filterAliasListByUTI(aList, targUTI) set newList to {} repeat with i in aList set j to POSIX path of i set tmpUTI to my retUTIfromPath(j) set utiRes to my filterUTIList({tmpUTI}, targUTI) if utiRes is not equal to {} then set the end of newList to j end if end repeat return newList end filterAliasListByUTI –指定のPOSIX pathのファイルのUTIを求める on retUTIfromPath(aPOSIXPath) set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value) if theResult = true then return theValue as string else return theResult end if end retUTIfromPath –UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList |
More from my site
(Visited 52 times, 1 visits today)