Batch export Apple Pages to PDF

Have some Applescript adapted from somewhere a few years ago, but it’s got a problem I can’t understand: it quits at the “export” line with the message error “The variable targetDocument is not defined.” number -2753 from “targetDocument”

Here’s the script - what’s wrong with the variable “targetDocument”? Thanks for any help!

set theSourceFolder to (choose folder with prompt “Select a folder whose files you wish to convert to pdf:”)

tell application “Finder”
set filesArray to every file of theSourceFolder whose name extension is “pages”
end tell

set theDestinationFolder to (choose folder with prompt "Select a folder where you want the converted files to be placed: ")

tell application “Pages” to activate

repeat with aFile in filesArray

tell application “Finder”
set fileAlias to aFile as alias
set fileName to name of fileAlias
set fileExtension to name extension of fileAlias
end tell

set theOriginalName to fileName

– remove extension

set prevTIDs to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “.” & fileExtension as string
set fileName to first text item of fileName
set AppleScript’s text item delimiters to prevTIDs

– prepare desktop folder path and name

set docPathAndName to theDestinationFolder & fileName & “.pdf” as string

tell application “Pages”

– open the file
set targetDocument to open aFile

– convert it
export targetDocument to docPathAndName as PDF

– close it
close targetDocument saving no

end tell

end repeat

tell application “Pages” to quit

display dialog “Done!”

Here is an edited and commented version.



set theSourceFolder to (choose folder with prompt "Select a folder whose files you wish to convert to pdf:")

tell application "Finder"
	set filesArray to every file of theSourceFolder whose name extension is "pages"
end tell

set theDestinationFolder to (choose folder with prompt "Select a folder where you want the converted files to be placed: ")

tell application "Pages" to activate

repeat with aFile in filesArray
	
	tell application "Finder"
		set fileAlias to aFile as alias
		set fileName to name of fileAlias
		set fileExtension to name extension of fileAlias
	end tell
	
	set theOriginalName to fileName
	
	-- remove extension
	
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "." & fileExtension as string
	set fileName to first text item of fileName
	set AppleScript's text item delimiters to prevTIDs
	
	-- prepare desktop folder path and name
	
	set docPathAndName to (theDestinationFolder as text) & fileName & ".pdf" # EDITED
	
	tell application "Pages"
		
		-- open the file
		set targetDocument to open fileAlias -- EDITED : was aFile which is a Finder reference, not what Pages requires
		
		repeat 10 times # I added a loop which may be useful if the original document is very long
			try
				export targetDocument to file docPathAndName as PDF
				exit repeat
			on error
				tell me to delay 0.5
			end try
		end repeat
		-- close it
		close targetDocument saving no
		
	end tell
	
end repeat

tell application "Pages" to quit

display dialog "Done!"

Here is a version which no longer use the Finder which I hate.
I no longer extract the extension name in the loop because we know that the treated items share the same extension defined at the very beginning.
I use a simpler code to drop the extension name from the full name.

set fileExtension to "pages"
set lastCharacterOffset to 2 + (count fileExtension)

set theSourceFolder to (choose folder with prompt "Select a folder whose files you wish to convert to pdf:") # returns an alias

tell application "System Events"
	set filesArray to path of every disk item of theSourceFolder whose name extension is fileExtension
end tell

set theDestinationFolder to (choose folder with prompt "Select a folder where you want the converted files to be placed: ") as text # This way we will not have to apply the coercion in every step of the loop

tell application "Pages"
	activate
	# filesArray is a list of HFS text paths
	repeat with filePath in filesArray
		
		-- open the file
		set targetDocument to open file filePath
		
		if filePath ends with ":" then
			set filename to item -2 of my decoupe(filePath, ":")
		else
			set filename to item -1 of my decoupe(filePath, ":")
		end if
		
		set theOriginalName to filename
		
		-- remove extension
		set filename to text 1 thru -lastCharacterOffset of filename
		# here theDestinationFolder is a text object
		set docPathAndName to theDestinationFolder & filename & ".pdf"
		
		repeat 10 times # I added a loop which may be useful if the original document is very long
			try
				export targetDocument to file docPathAndName as PDF
				exit repeat
			on error
				tell me to delay 0.5
			end try
		end repeat
		-- close it
		close targetDocument saving no
		
	end repeat
	
	quit
end tell

display dialog "Done!"

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) lundi 11 juillet 2016 19:45:23

Many thanks, Yvan, for both the fix and the improvements.

Now the export step is complaining that I don’t have permission, though a manual export from within Pages proceeds with no problem. Maybe I need to set a parameter for “export”?

I guess that there is a problem with Security & Privacy.

Open the so called System Preferences Pane
Select the rightmost tab (named Confidentialité in French)
Select the menu item Accessibility

If you run the script from the Script Editor, check that this one appears in the scrolling area displayed on the right side of the window.
If it’s not, open the lock then add Script Editor to the scroll area.

If you run the script as an application, check that this one appears in the scrolling area displayed on the right side of the window.
If it’s not, open the lock then add the script application to the scroll area.

As I run most of my scripts thru FastScripts, I also added FastScripts to the scroll area.

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mardi 12 juillet 2016 19:19:24

Hmm-m-m. Added both Script Editor and Pages to the Accessibility list, but the permission problem persists.

OK - I found a hint by googling the permission problem. Where I had:

export targetDocument to docPathAndName as PDF

I should have instead:

export targetDocument to file docPathAndName as PDF

Making that change, the script now works. Thanks for all your help. FYI, I elaborated the export line a bit:

export targetDocument to file docPathAndName as PDF with properties {image quality:Best}

I was unable to guess that because the word file is written in my code :wink:

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mardi 12 juillet 2016 21:10:44

Thanks for the script! I was was able to modify your script to batch convert pages documents to word docx files. Your script is fantastic! Thank you!