How to import pages in Acrobat 5.0

Hi I have been trying to make a script which would automatically merge all pdf documents in a folder. I found 1 here in Macscripter.com but doesn’t seem to work. I know it might be something extremely easy but I don’t see it. I have tried in various ways and the script always works up until the line

insert pages document 1 from document 2 starting with…

the response is always “document 1 does not understand insert pages”

Can someone please help me?

Obed

Using Acrobat, you can try the following code:

and you could also look at join_PDFs or other utilities posted on ScriptBuilders.

Jon

This is the error message it gives:

Finder got an error: Can’t set file “System:Users:firstuser:Desktop:combined.pdf” to alias “System:Users:firstuser:Desktop:second.pdf”.

I was thinking of just using the very first document as the combined.pdf document and just keep on opening the rest of the documents and adding them on. This is what I had done before:

tell application “Finder”
set myFolder to (choose folder with prompt “Which folder contains pages to be merged?”) as alias
set myCount to (count (items in myFolder whose name ends with “.pdf”))
set i to 1
set theDoc to name of item 1 of myFolder whose name ends with “.pdf”
end tell

tell application “Acrobat 5.0”
open file ((myFolder as text) & theDoc as text)
set toDoc to document 1 as reference
repeat
set i to i + 1
tell application “Finder”
set thePages to name of item i of myFolder whose name ends with “.pdf”
end tell
if i > myCount then
exit repeat
end if
open file ((myFolder as text) & thePages as text)
set fromDoc to document 2 as reference
set DocPageCount to count theDoc each PDPage
set InsertCount to count of every PDPage of thePages
insert pages document 1 from document 2 starting with 1 number of pages InsertCount after DocPageCount
close document 2 saving no
end repeat
end tell

it works up to the insert pages part.

Sorry, that should have been:

Jon

It wasn’t working at first but I changed a few lines and then it worked. The assumption is that once document 2 is closed and one opens the next item i that it would again be document 2 but it wasn’t the case, so I just changed every document 2 reference to document i.

Thanks for your help. It helped me a lot. I appreciate it.

here is the script with the suggested revision and something i have added to created bookmarks for each added pdf; there is probably a more elegant way to do this but this is the first script i have seen that works (i am using acrobat 5 on osX.2.6 and i have ui scripting activated):



set target_folder to ((path to desktop) as string)
set target_name to "combined.pdf"
set target_pdf to target_folder & target_name
set source_folder to (choose folder) as alias

try
	tell application "Finder"
		set component_PDFs to every file of source_folder whose name ends with "pdf"
		try
			delete file target_pdf
		end try
		set the_name to name of (item 1 of component_PDFs as alias)
		duplicate (item 1 of component_PDFs as alias) to folder target_folder
		set name of file (target_folder & the_name) to target_name
	end tell
	
	tell application "Acrobat 5.0"
		activate
		open (target_pdf as alias)
		
		tell application "System Events"
			tell process "Acrobat 5.0"
				keystroke "b" with command down -- to mark first page
			end tell
		end tell
		
		repeat with i from 2 to (count of component_PDFs)
			open (item i of component_PDFs as alias)
			
			tell application "System Events"
				tell process "Acrobat 5.0"
					keystroke "b" with command down -- to mark other pdfs as they are inserted
				end tell
			end tell
			
			set combined_page_count to (count PDPage of document 1)
			set source_page_count to (count PDPage of document i)
			insert pages document 1 after combined_page_count from document i starting with 1 number of pages source_page_count with insert bookmarks
			close document i saving no
		end repeat
		close document 1 with saving and linearize
	end tell
on error the_error
	beep
	set the_button to button returned of (display dialog the_error buttons {"Copy to Clipboard", "OK"} default button 2 with icon 0 giving up after 30)
	if the_button = "Copy to Clipboard" then set the clipboard to the_error
end try


I tried to get the “insert pages” command to work over and over and I always got the error “document xxx doesn’t understand the insert pages event,” or something to that effect. I tried things like…

insert pages document “one.pdf” after 1 from document “two.pdf” starting with 1 number of pages 1
insert pages document 1 after 1 from document 2 starting with 1 number of pages 1

set xxx to active doc
set zzz to document “test.pdf”
insert pages xxx after 1 from zzz starting with 1 number of pages 1

…and they all failed with that error. However, when I ran your script, it worked. Not only that, it somehow fixed the “insert pages” command – the above lines, which always used to fail in my scripts, all suddenly work perfectly, now. Why is this? Anyways, thanks for your magical script!

Greg.