Batch convert Pages files to PDF

Hi there,

I have the same problem like him

https://discussions.apple.com/thread/251333833?answerId=252585328022#252585328022 and get the final dialogue showing a total count of 0 and files fail to convert. I used only Pages files (5.6.2) but I'm still running macOS 10.11. Compiled the code exactly as instructed and saved it as a program on the desktop. Would anyone have a tip on how to change the code for OS X 10.11?



Mac mini, OS X 10.11

Posted on Apr 17, 2021 2:29 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 17, 2021 11:18 AM

I just copied and pasted the two parts of the Pages to PDF AppleScript, from the linked post, into Script Editor on El Capitan 10.11.6 and saved it as an application to my Desktop. I then tested it with nine very different Pages documents, as well as these same nine documents in a folder that I also dropped on the application. Every single Pages document was correctly exported to PDF.


This script does not work correctly when Pages documents are dropped on it in macOS 11.2.3.


Here is the Pages to PDF AppleScript in two parts as before, that must be pasted into the Script Editor with a blank line between the appending:


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)
				else
					log "ignore" # not a folder or pages document
				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
	if fileCnt > 0 then
		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
	else
		display dialog "Export completed without any files processed." & return & "Only Pages (.pages) documents are exported to PDF." buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias
	end if
	quit
end open


Similar questions

4 replies
Question marked as Top-ranking reply

Apr 17, 2021 11:18 AM in response to ricido

I just copied and pasted the two parts of the Pages to PDF AppleScript, from the linked post, into Script Editor on El Capitan 10.11.6 and saved it as an application to my Desktop. I then tested it with nine very different Pages documents, as well as these same nine documents in a folder that I also dropped on the application. Every single Pages document was correctly exported to PDF.


This script does not work correctly when Pages documents are dropped on it in macOS 11.2.3.


Here is the Pages to PDF AppleScript in two parts as before, that must be pasted into the Script Editor with a blank line between the appending:


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)
				else
					log "ignore" # not a folder or pages document
				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
	if fileCnt > 0 then
		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
	else
		display dialog "Export completed without any files processed." & return & "Only Pages (.pages) documents are exported to PDF." buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias
	end if
	quit
end open


Apr 17, 2021 12:42 PM in response to ricido

Since the code still works for me on El Capitan 10.11.6 with Pages v5.6.2, when copied from the earlier post, I suspect you may not have copied all of the code correctly, or it is how you are attempting to drag and drop it onto the Desktop application icon.


The code expects only Pages documents, whether singularly, multiples, or a folder of them. No other document type will be processed and would result in that dialog that you shared.


When you drag and drop one or more Pages documents over the application icon, wait for it to be surrounded by a rectangular semi-transparent border before you release the drag. See if that makes a difference. If not, then start over, and copy/paste the two parts of AppleScript I posted above into the Script Editor and save as an application to your Desktop. Retry.

Apr 17, 2021 11:12 AM in response to VikingOSX

Part 2


on export_file(afile)
	-- switch extension from .pages to .pdf
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ".pages"}
	set exportDocument to item 1 of text items of (afile as alias as text) & ".pdf"
	set AppleScript's text item delimiters to TID
	
	close access (open for access exportDocument)
	tell application "Pages"
		try
			set myDoc to open afile
			with timeout of 1800 seconds
				if (version of it) ≥ "5.6" then
					export myDoc to file exportDocument as PDF with properties {image quality:Best}
				else
					export myDoc to file exportDocument as PDF
				end if
			end timeout
			set fileCnt to (fileCnt + 1) as integer
			close myDoc saving no
		on error errmsg number errnbr
			my error_handler("Export file", errnbr, errmsg)
			quit
		end try
	end tell
	-- force extension visibility on exported PDF
	tell application "Finder"
		if exists (item exportDocument as alias) then
			set extension hidden of (item exportDocument as alias) to false
		end if
	end tell
	return
end export_file

on error_handler(nbr, msg, handler_name)
	return display alert handler_name & ": " & "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler

on quit {}
	-- perform these cleanup items, and then really quit
	tell application "Pages" to if it is running then quit
	set fileCnt to (0 as integer)
	set exportDocument to ""
	continue quit
end quit


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Batch convert Pages files to PDF

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.