Numbers export PDF broken?

Hi there friends,

I’ve been trying to export a PDF from Numbers and it seems to not work unless Numbers has been given permission in some way to access the destination folder. This permission can be given either:

  1. by exporting a PDF manually from Numbers to that location; or
  2. get Numbers to ask the user to choose the destination folder via a choose folder dialog.

To illustrate, try the following script with any Numbers document currently open. It should export a PDF called Untitled1.pdf to your desktop.


set testFileName to "Untitled1.pdf"
set destinationFolder to (path to desktop) as text
set theFilePath to destinationFolder & testFileName

tell application "Numbers"
	export document 1 as PDF to theFilePath
end tell

I tried this on several Macs running different operating systems and it refused to work. I even tried creating a blank file first (which has saved me previously). e.g.


set fRef to (open for access file theFilePath with write permission)
close access fRef

This creates a blank PDF in the location but Numbers still refused to overwrite it unless one of the two conditions above are executed.
Weird eh?

You can edit following script to export the document only as PDF. Making the new empty file with Finder allows to avoid any security problems when exporting. Also, as I noticed, the Numbers.app doesn’t export to none existing files.


-- script: Batch Export Numbers Document

set destinationFolder to (path to desktop) as text

tell application "Numbers"
	set docName to name of document 1
	set CSVExportFileName to destinationFolder & docName & "_Exported:" & docName & ".csv"
	set XLSExportFileName to destinationFolder & docName & "_Exported:" & docName & ".xls"
	set numbersExportFileName to destinationFolder & docName & "_Exported:" & docName & ".numbers"
	set PDFExportFileName to destinationFolder & docName & "_Exported:" & docName & ".pdf"
end tell

tell application "Finder"
	try
		set theFolder to make new folder at destinationFolder with properties {name:(docName & "_Exported")}
	on error
		set theFolder to folder (destinationFolder & docName & "_Exported")
	end try
	try -- the most important piece (block) of code
		make new file at theFolder with properties {name:(docName & ".csv")}
		make new file at theFolder with properties {name:(docName & ".xls")}
		make new file at theFolder with properties {name:(docName & ".numbers")}
		make new file at theFolder with properties {name:(docName & ".pdf")}
	end try
end tell

tell application "Numbers"
	activate
	export document 1 to file CSVExportFileName as CSV
	export document 1 to file XLSExportFileName as Microsoft Excel
	export document 1 to file numbersExportFileName as Numbers 09
	export document 1 to file PDFExportFileName as PDF
end tell

Hi KniazidisR,

thanks for your time. Your script worked for all except XLS files – Numbers got an error: “.xls”.
Maybe because the name had “.numbers.xls”.

Anyhow, I don’t know what happened but after running your script I cannot get Numbers to NOT work anymore. That is to say, the original problem is now gone. I’m at a loss because it was working properly on Monday, then for the last 24 hours not at all (apart from the workarounds mentioned above), and now it’s working properly again!

So if nobody else is able to recreate the error, it must be some strange compilation error on my computer.

Restarting the application or script editor or the computer solves many strange problems. Also, some old scripting additions may conflict with application terms.

davidmorgan. KniazidisR has answered your question but I thought I would comment on one point. Your script in post 1 did not work for me and returned the following error (I changed the target file name):

I then tried again but identified theFilePath as a file and the script worked correctly (once again I changed the file name to start new):

set testFileName to "Test 2.pdf"
set destinationFolder to (path to desktop) as text
set theFilePath to destinationFolder & testFileName

tell application "Numbers"
	export document 1 as PDF to file theFilePath
end tell

I do not remember now in which of the latest versions of OSX Numbers.app refused to export the document to a file that does not yet exist. Then the script given above was written by me. Maybe, it was specific version’s bug.

On Catalina, @peavine’s script works correctly, without the need to create an empty file first.