insert page: "document doesn't understand insert pages message

Hi guys,

this is my script and I keep getting this error - I have been trying around all day but don’t seem to get it to work.
here is the thing: I am running 2 if cases here, every time I remove one of them ( to only insert one document ) it works fine. so the code can’t be that wrong. I got it to work before with two ifs but somehow i got stuck with the real pdf documents I need to insert. Any idea of what is wrong??
I have already copied part of a script I found here in a post from 2007, but it does not fix the problem.

set mainfolder to choose folder
tell application "Finder" to set folderList to (sort folders of mainfolder as alias list by name)
display dialog "Ordner docs wählen"
set source_folder to choose folder

repeat with i from 1 to count of folderList
	try
		tell application "Finder" to set Master_File to (files of entire contents of (item i of folderList))
	end try
	
	
	repeat with i from 1 to count of folderList
	try
		tell application "Finder" to set Master_File to (files of entire contents of (item i of folderList))
	end try
	
	
	tell application "Finder" to set file_list to (files of entire contents of source_folder)
	
	tell application "Adobe Acrobat Pro"
		open Master_File
		set Master_Doc to document 1
		set PageCount to 0
	end tell
	
	repeat with this_file in file_list
		tell application "Adobe Acrobat Pro"
			if name of this_file is "Deckblatt.pdf" then
				open (this_file as alias)
				insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 1
				close document 2 without saving
			end if

			if name of this_file is "Anhang.pdf" then
				open (this_file as alias)
				set PageCount to 2
				
			insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 5
				
				close document 2 without saving
			end if
			
		end tell
	end repeat
	
	tell application "Adobe Acrobat Pro"
		
		close Master_Doc saving yes
	end tell
	
	
end repeat

Help is highly appreciated! thanks guys :expressionless:

Hi,

There are some issues

First of all, why is the first repeat loop written twice ?

Master_File is a single or a list of Finder file specifiers, which Acrobat cannot open.
In an Acrobat tell block a single Finder specifier file is opened in Preview.app. Multiple files throw an error.

Assuming Master_File is a single Finder specifier file the most reliable way to get the reference in Acrobat is


tell application "Adobe Acrobat Pro"
	set masterFileName to name of Master_File
	open (Master_File as text)
	set Master_Doc to document masterFileName
	set PageCount to 0
end tell


thanks, Stefan!
Sorry, the repeated if loop was a copy-paste mistake I made here in the post.

so your answer led me to do this, which works just as I wanted it:

display dialog "Ordner xxx wählen"
set mainfolder to choose folder
tell application "Finder" to set folderList to (sort folders of mainfolder as alias list by name)
display dialog "Ordner docs wählen"
set source_folder to choose folder

repeat with i from 1 to count of folderList
	try
		tell application "Finder" to set Master_File to (files of entire contents of (item i of folderList))
	end try
	
	
	
	tell application "Finder" to set file_list to (files of entire contents of source_folder)
	
	tell application "Adobe Acrobat Pro"
		set masterFileName to name of Master_File
		open (Master_File as text)
		set Master_Doc to document masterFileName
		set PageCount to 0
	end tell
	
	repeat with this_file in file_list
		tell application "Adobe Acrobat Pro"
			if name of this_file is "Deckblatt.pdf" then
				open (this_file as alias)
				insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 1
				close document 2 without saving
			end if
		end tell
	end repeat
	
	tell application "Adobe Acrobat Pro"
		
		close Master_Doc saving yes
	end tell
end repeat

repeat with i from 1 to count of folderList
	try
		tell application "Finder" to set Master_File to (files of entire contents of (item i of folderList))
	end try
	
	
	
	tell application "Finder" to set file_list to (files of entire contents of source_folder)
	
	tell application "Adobe Acrobat Pro"
		set masterFileName to name of Master_File
		open (Master_File as text)
		set Master_Doc to document masterFileName
		set PageCount to 2
	end tell
	
	repeat with this_file in file_list
		tell application "Adobe Acrobat Pro"
			if name of this_file is "Anhang.pdf" then
				open (this_file as alias)
				insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 5
				close document 2 without saving
			end if
		end tell
	end repeat
	
	tell application "Adobe Acrobat Pro"
		
		close Master_Doc saving yes
	end tell
end repeat

I did not know about only being able to open one file so now I just repeated the whole loop! Sometimes it just takes a little nudge…
thanks again!:slight_smile:

Master_File is obviously a singe file. Could you locate it more exactly, because entire contents is very expensive.
It’s probably not necessary to call the whole repeat loop twice, try this (untested)


display dialog "Ordner xxx wählen"
set mainfolder to choose folder
tell application "Finder" to set folderList to (sort folders of mainfolder as alias list by name)
display dialog "Ordner docs wählen"
set source_folder to choose folder

repeat with i from 1 to count of folderList
	try
		tell application "Finder"
			set Master_File to (1st file of entire contents of (item i of folderList))
			set file_list to (files of entire contents of source_folder)
		end tell
	on error
		display dialog "Master File konnte nicht gefunden werden" buttons {"Cancel"} default button "Cancel"
	end try
	
	tell application "Adobe Acrobat Pro"
		set masterFileName to name of Master_File
		open (Master_File as text)
		set Master_Doc to document masterFileName
		set PageCount to 0
	end tell
	
	repeat with this_file in file_list
		tell application "Adobe Acrobat Pro"
			open (this_file as alias)
			if name of this_file is "Deckblatt.pdf" then
				set PageCount to 0
				insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 1
			else if name of this_file is "Anhang.pdf" then
				set PageCount to 2
				insert pages Master_Doc after PageCount from document 2 starting with 1 number of pages 5
			end if
			close document 2 without saving
		end tell
	end repeat
	
	tell application "Adobe Acrobat Pro"
		
		close Master_Doc saving yes
	end tell
end repeat