Hi all,
I have taken apart a couple of different PDF’s page by page, and then rearranged those pages into a new list. But now I have myself in the deep waters and finding that I don’t know how to swim as well as I thought.
First the code to split apart each of these PDF’s.
set posixPath to POSIX path of (savePDFPath as alias)
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of posixPath))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
set thePages to {}
repeat with e from 1 to newDocCount
set e1 to e - 1
set thePage to (newDoc's pageAtIndex:(e1))
copy thePage to end of thePages
end repeat
That code gives me a list of all the pages from a single PDF, I have done this several times with several different PDF’s and then put those pages into a new order in a new list that looks like this:
I have tried to add the following code to my script:
property |NSURL| : a reference to current application's |NSURL|
property PDFDocument : a reference to current application's PDFDocument
set posixNewpath to "/Users/Chris/Documents/"
set destPosixPath to (posixNewpath & "empty.pdf")
set aURL to (|NSURL|'s fileURLWithPath:destPosixPath)
set aPDFdoc to (PDFDocument's alloc()'s initWithURL:aURL)
set pdfRes to (aPDFdoc's writeToFile:fullDoc) as boolean
with empty.pdf being actual file, but when I try this code in my script I get an “Unrecognized Selector” for the last line of code. So I am missing SOMETHING, I just can’t figure out what that might be.
Sorry Shane, I was mistaken in my OP. I had been getting the missing selector when I was trying to use a modified version of the code you had written to combine PDF’s, but since that was looking for actual files on the disk to combine, I figured out that it probably wasn’t going to work for what I was attempting to do. It was that code that was giving me the missing selector, very sorry for the mix up.
I remembered that there was some other code that I believe you also authored, for filling out PDF forms that just wrote back to the original PDF. That is where I took the code for writing to the PDF from. On that code I am getting a missing value for aPDFdoc, which ultimately points back to the empty.pdf file. I have tried the code both on an existing PDF, empty.pdf, and I have tried pointing it to a non-existent file, empty2.pdf file. In both cases, I get the error that “missing value doesn’t understand the “writeToFile_” message.” Looking through variables in SD, I see that aPDFDoc is what has the missing value.
If I understand what you’re trying to do correctly, what you need to do is create a new blank document using alloc()'s init(), and then insert the pages using insertPage:atIndex:.
Here is the code that actually did what I was looking for it to do. I believe this was originally authored by Shane as well
set posixNewpath to "/Users/Chris/Documents/"
set newDocName to "new.pdf"
set outFolderNSURL to (current application's |NSURL|'s fileURLWithPath:posixNewpath)
set outNSURL to (outFolderNSURL's URLByAppendingPathComponent:newDocName)
set theNewPDFDocument to current application's PDFDocument's new()
repeat with p from 1 to (count of items of fullDoc)
set thePDFPage to item p of fullDoc
(theNewPDFDocument's insertPage:thePDFPage atIndex:0)
end repeat
(theNewPDFDocument's writeToURL:outNSURL)