Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

AppleScript to convert .pages to .pdf

Hello,

can anyone produce a working script that can export pages document to pdf by drag and drop?


There are examples online, and even chatGPT can produce such script.


Unfortunately none of it seems to be working. It runs the script (without errors) but it doesn’t produce the PDF file.

MacBook Pro 14″, macOS 13.2

Posted on Mar 25, 2023 3:24 AM

Reply

Similar questions

8 replies

Mar 25, 2023 6:27 AM in response to Alex-332

Alex-332 wrote:

Hello,
can anyone produce a working script that can export pages document to pdf by drag and drop?

There are examples online, and even chatGPT can produce such script.

Unfortunately none of it seems to be working. It runs the script (without errors) but it doesn’t produce the PDF file.


Why not use the Export as PDF function...(?)





Ref:

Pages User Guide for Mac


Batch convert .pages to .pdf - Apple Community


https://www.ghostotter.com/using-automator-convert-pages-pdf/


https://gist.github.com/loop/7207134ed7ff7a288ee1



Mar 25, 2023 1:09 PM in response to VikingOSX

Here is an AppleScript that I have tested with Pages v12.2.1 on macOS Monterey v13.2.1. This has been tested by dragging one or more Pages documents onto the Desktop application drop well, and also with a Folder of Pages documents. It just works, though you will encounter some initial Ventura security dialogs that you should choose Okay so the appropriate settings are made in the Settings > Security & Privacy panel. See my response to Mr Hoffman earlier.


Copy and paste the following code into the Script Editor and click the hammer (compile) icon. Don't run it yet. Then you need to save it twice: 1) as AppleScript source (text), and 2) by pressing the option key and choosing Save As… as an application to your Desktop. This will have an embossed down arrow on it to indicate you drop stuff on it.


Code:


use scripting additions

property valid_kind : {"Pages Document"}
property imageBest : false
property fileCnt : (0 as integer)
property dx_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

on open dropped_items
	
	tell application "Pages" to set itsversion to its version
	considering numeric strings
		if itsversion > "5.6" then set imageBest to true
	end considering
	
	repeat with anItem in dropped_items
		try
			tell application "Finder"
				
				if kind of anItem 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 kind of anItem is in valid_kind then
					my export_file(anItem)
				else
					log "ignored" -- think of this as a continue statement
				end if
			end tell
		on error errmsg number errnbr
			my error_handler(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 dx_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 dx_icon
	end if
	quit
end open

on export_file(afile)
	-- switch pages extension to pdf
	set thisFile to afile as alias as text
	set exportPDF to (text 1 thru ((offset of ".pages" in thisFile) - 1) of thisFile as text) & ".pdf"
	close access (open for access exportPDF)
	tell application "Pages"
		try
			set myDoc to open afile
			with timeout of 1800 seconds
				if imageBest then
					export myDoc to file exportPDF as PDF with properties {image quality:Best}
				else
					export myDoc to file exportPDF 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(errnbr, errmsg)
			quit
		end try
	end tell
	-- an interactive Export To PDF in Pages makes the PDF extension visible
	-- but this script needs to be coerced to do that.
	tell application "Finder"
		if exists (item exportPDF as alias) then
			set extension hidden of (item exportPDF as alias) to false
		end if
	end tell
	return
end export_file

on error_handler(nbr, msg)
	return display alert "[ " & 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)
	continue quit
end quit


Mar 25, 2023 11:45 AM in response to Alex-332

Are you okay with the PDFs being written in the same location as the dropped Pages documents (or the folder of Pages documents)?


This drop solution can be an AppleScript or Automator application that perches on your Desktop.


I have the code written to export Pages documents to PDF and would just need to tailor it to your needs and test it on Ventura 13.2.1.

AppleScript to convert .pages to .pdf

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