There was nothing wrong with the code when it was posted, but there is now, as the transition from the old hosting software to its replacement has collapsed white-space in parts of the code and introduces extra newlines when copy/pasted. Rather than hunt all these down, I will post newly written and tested code (from yesterday) in two parts that you append. That is because the new hosting software refuses to post longer code examples, or allow us to customize our post to create vertically scrollable windows.
Same outline steps as before, though the Script Editor has done away with step 3.2.4. Copy/paste the code that follows Part 1 into the script editor, add an extra line, and then copy/paste the code below Part 2 after the preceding code. Then click the hammer button to compile and then save per the outline steps.
Longer code scrolls horizontally, so I suggest that you click and drag downward to select all of the code, and not just what is displayed.
Part 1
-- Pages2PDF.applescript
-- Drag and drop file(s), or folder(s) onto application icon to export PDF at source location
-- Requires Pages v5.6 or later thru Pages 10 (tested)
-- Tested: macOS 10.14.6, 10.15.4
-- VikingOSX, 2020-05-04, Apple Support Communities, No warranties expressed or implied
use scripting additions
property valid_kind : {"Pages Publication", "Pages document"}
property fileCnt : (0 as integer)
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"
on open dropped_items
tell application "Pages" to set aversion to its version
considering numeric strings
if aversion < "5.6" then
display alert "You need Pages v5.6 or later to run this script" giving up after 10
quit
end if
end considering
repeat with anItem in the dropped_items
try
tell application "Finder"
set akind to kind of anItem
if akind is equal to "Folder" then
set docList to (every item in entire contents of folder anItem ¬
whose kind is in valid_kind) as alias list
repeat with afile in docList
my export_file(afile)
end repeat
else if akind is in valid_kind then
my export_file(anItem)
end if
end tell
on error errmsg number errnbr
set errval to "Dropped Items repeat - " & (anItem as text)
my error_handler(errval, errnbr, errmsg)
quit
end try
end repeat
display dialog "Export Complete." & return & tab & " Files exported to PDF " & " : " & (fileCnt as text) ¬
buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias
quit
end open