macOS Sierra: Export PDFs from Pages using AppleScript

It appears that macOS Sierra introduces a bug in Pages which means that you get a sandbox/permissions related error when trying to export a PDF using AppleScript.
Hopefully, Apple will fix it soon. However, until then, here’s a workaround (see sample below in a handler suitable for Automator).
The idea is to take advantage of the fact that the Pages file is a package (in effect, a folder) and temporarily save the PDF there, having done that, we move it to its correct location using the Finder. Scruffy, but it works.
on run {input, parameters}
repeat with theFile in input
tell application "Finder"
set theFilesFolder to (folder of theFile) as text
end tell
tell application "Pages"
set theDoc to open theFile
set theDocName to name of theDoc
set theName to (characters 1 thru -7 of theDocName) as text
set thePDFPath to ((theFile as text) & theName & ".pdf") as text
export theDoc to thePDFPath as PDF
close theDoc
end tell
tell application "Finder"
move file thePDFPath to folder theFilesFolder with replacing
end tell
end repeat
end run
While we have your attention… why not check out our macOS apps?
Barcode Basics – macOS barcode generator (including Automator support)
Ai Actions – Automator action pack for Adobe Illustrator
Pages Automator Actions – Automator action pack for Pages