Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Batch convert Pages files to PDF

Hi, I would like to convert several Pages files to PDF, in one go. I found an answer from VikingOXS dated 2017, but it doesn't work for me. I tried "replying" to that post but the Reply box was greyed out. That answer was an Applescript, but when I try compiling I get an error. I'm using Pages version 10, on MacOS Catalina 10.15.4. I can re-post the suggested script, and the compile error that I get, if that would help.

Mane Thanks.... Peter

Mac Pro

Posted on May 3, 2020 9:31 PM

Reply
Question marked as Best reply

Posted on May 5, 2020 4:26 AM

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


13 replies
Question marked as Best reply

May 5, 2020 4:26 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


May 5, 2020 4:28 AM in response to Peterh34

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


May 8, 2020 4:55 AM in response to dergitarrist

Since you are not receiving any syntax error, and the script is running to conclusion, it causes me to wonder about:

  1. I do not select either option associated with saving as an application
  2. The application is saved to the Desktop, and not run from the Dock
  3. I wait for the dark surround to appear behind the application when dragging files onto it, before releasing the mouse.
  4. Files are dragged from the local drive, or iCloud Drive onto the application, and not from any other cloud service such as Dropbox, Microsoft One Drive, etc.). Don't have them, and untested.


I can generate your Export Complete 0 dialog by dragging and dropping a text file onto the application. It is written to convert only Pages documents ending with .pages into PDF.

May 5, 2020 4:54 AM in response to VikingOSX

The first time that you attempt drag and drop of Pages content onto the saved application, you will encounter the following dialogs on Mojave and Catalina. Click OK to each, and subsequent usage should be without these interruptions. If you click "Don't allow", the application will no longer work.




And a succesful conversion results in the following dialog with a count of those files exported to PDF:


May 8, 2020 7:03 AM in response to Peterh34

I have added some code changes to the script to ignore any file that is not a Pages .pages document:



The loop will continue now without any confusion about ignored non-Pages documents.


Also, the previous side-effect of dropping a non-Pages document would be a completion dialog using the file counter still set to zero, as no export processing took place. I have now added a test for a valid count with a requisite dialog displayed.



And the new dialog when one non-Pages document is dropped on the application:



If a mixture of Pages and non-Pages documents are dropped, the non-Pages documents will be ignored, and only the Pages documents will be processed with the normal dialog counting the converted PDF.

May 5, 2020 10:17 AM in response to dergitarrist

I cannot reproduce the dialog you show with a 0 count.


I copied and appended the two parts of the posted code above into Script Editor on both Mojave 10.14.6, and Catalina 10.15.4. I then compiled it, and saved as an application (no options selected) on both platform's Desktop.


On Mojave and Catalina, selecting multiple Pages documents and dropping on the application resulted in a matching count of PDF conversions. On both platforms, dragging and dropping a folder of Pages documents resulted in a matching count of PDF in that folder too. However, dragging a mixture of Pages documents from the Desktop, and a folder of Pages documents at the same time onto the application will finish with the wrong count, and one PDF that is empty.


The behavioral problem of drag/drop both files and folders dates back several years, and no changes to the code will resolve how Apple handles mixed object drag and drop items and AppleScript. The recommendation is not to mix files and folders in the drop operation.

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 ID.