Script - Convert Apple Pages document to PDF

This line is pointing to the system Trash…

set theFolder to path to trash as string


Here is my take on this, as a teaching edition script…

tell application id "DNtp"
	set tempFolder to (path to temporary items from user domain) as string
	-- Using the temp folder for temporary exports - a common thing to do.
	
	repeat with thisRecord in (selection as list)
		set recName to (name of thisRecord) -- Get the record's name
		if recName ends with ".pages" then -- If the name ends with ".pages", process it
			set baseName to (characters 1 thru -7 of recName) as string -- Get all the characters before the ".pages" and coerce it back to a string
			set exportName to (baseName & ".pdf") as string -- Make a new string with the proper file extension
			
			-- Calling Pages in a separate handler
			set convertedDoc to my pagesConvertToPDF(path of thisRecord, tempFolder, exportName)
			-- Passing the path, the location of the temp folder for exporting, and the name to be used for the export.
			
			-- After the handler finishes, the script continues here… 
			import (tempFolder & exportName) to current group -- Import to the current group.
		end if
	end repeat
end tell

on pagesConvertToPDF(recPath, tempFolder, exportName)
	tell application "Pages"
		set theDoc to open recPath -- Open the document
		with timeout of 1800 seconds -- Give it three minutes to export, as a safety margin
			export theDoc to file (tempFolder & exportName) as PDF -- Exporting to the temp folder, with the proper name, and as a PDF
		end timeout
		close theDoc -- Close the document when finished
	end tell
	-- Return back to where the script left off
end pagesConvertToPDF

This shows an example of a simple handler with named parameters.
Also, this is not error-trapped outside of checking if a document is a Pages file or not.