— Created 2016-09-20 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "Quartz"
use framework "AppKit"
set aExt to ".jpg"
set targAlias to retFrontFinderWindowsTargetIfExits(path to desktop) of me
set aFol to choose folder with prompt "追記するJPEG画像ファイルが入っているフォルダを選択" default location targAlias
set fList to getFilePathList(aFol, aExt) of me
set f2List to my sort1DList:fList ascOrder:true –sort by ascending
set newFile to POSIX path of (choose file of type {"com.adobe.pdf"} with prompt "既存のPDFファイルを選択(このPDF末尾に画像を追加)")
set newFilePath to current application’s NSString’s stringWithString:newFile
set newFileURL to current application’s |NSURL|’s fileURLWithPath:newFile
–Get Exsisting PDF’s URL and Use it
set aPDFdoc to current application’s PDFDocument’s alloc()’s initWithURL:newFileURL
set pageNum to ((aPDFdoc’s pageCount()) as integer)
repeat with i in f2List
set j to contents of i
set aURL to (current application’s |NSURL|’s fileURLWithPath:j)
set bImg to (current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL)
(aPDFdoc’s insertPage:(current application’s PDFPage’s alloc()’s initWithImage:bImg) atIndex:pageNum)
set pageNum to pageNum + 1
end repeat
aPDFdoc’s writeToFile:newFilePath
–ASOCで指定フォルダのファイルパス一覧取得(拡張子指定つき)
on getFilePathList(aFol, aExt)
set aPath to current application’s NSString’s stringWithString:(POSIX path of aFol)
set aFM to current application’s NSFileManager’s defaultManager()
set nameList to (aFM’s contentsOfDirectoryAtPath:aPath |error|:(missing value)) as list
set anArray to current application’s NSMutableArray’s alloc()’s init()
repeat with i in nameList
set j to i as text
if (j ends with aExt) and (j does not start with ".") then –exclude invisible files
set newPath to (aPath’s stringByAppendingString:j)
(anArray’s addObject:newPath)
end if
end repeat
return anArray as list
end getFilePathList
–1D List(文字)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DList:theList ascOrder:aBool
set aDdesc to current application’s NSSortDescriptor’s sortDescriptorWithKey:"self" ascending:aBool selector:"localizedCaseInsensitiveCompare:"
set theArray to current application’s NSArray’s arrayWithArray:theList
return (theArray’s sortedArrayUsingDescriptors:{aDdesc}) as list
end sort1DList:ascOrder: