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

The scriptable export in Numbers do not always give errors that make sense. When the mind is thinking ‘What is wrong’ and ‘Why do it not work’ and so on…

Here is my script to export in Monterey for all the formats…

Ps.
You could do something like this file ((path to desktop as text) & “Budet” & “.pdf”) as PDF
But somethimes its more confusing with all the bracket to make AppleScript to not complain.

You could run it and it will create Budget documents in Dekstop folder

tell application "Numbers"
	make new document with properties {document template:template "Personal Budget"}
end tell

set fileFormats to {"PDF", "Microsoft Excel", "CSV", "Numbers 09"}

tell application "Numbers"
	-- Get the name of the sheet
	set documentName to its name of sheet 1 of front document
	-- Set the parent folder
	set theFolder to (path to desktop)
	-- Append the parent folder with the sheet name
	set targetFilePath to (theFolder & documentName as text)
	
	repeat with fileFormat in fileFormats
		if contents of fileFormat is "PDF" then
			export front document to file (targetFilePath & ".pdf") as PDF
		end if
		if contents of fileFormat is "Microsoft Excel" then
			export front document to file (targetFilePath & ".xlsx") as Microsoft Excel
		end if
		-- This one will create a folder with csv's if > 1 sheet
		if contents of fileFormat is "CSV" then
			export front document to file (targetFilePath & ".csv") as CSV
		end if
		(* This one is not scriptable with AppleScript
		if contents of fileFormat is "TSV" then
			export front document to file (targetFilePath & ".tsv") as TSV
		end if
		*)
		if contents of fileFormat is "Numbers 09" then
			export front document to file (targetFilePath & ".numbers") as Numbers 09
		end if
	end repeat
end tell

Here is also a example using POSIX path of. In the dictionary of Numbers it say file it means the path need to be alias path HFS “:” but if we like to use POSIX path instead “/” we use POSIX file. Also theFolder variable is alias and comes before documentName we also need as text for our POSIX path of command.

tell application "Numbers"
	make new document with properties {document template:template "Personal Budget"}
end tell

set fileFormats to {"PDF", "Microsoft Excel", "CSV", "Numbers 09"}

tell application "Numbers"
	-- Get the name of the sheet
	set documentName to its name of sheet 1 of front document
	-- Set the parent folder
	set theFolder to (path to desktop)
	-- Append the parent folder with the sheet name
	set targetFilePath to POSIX path of (theFolder & documentName as text)
	
	repeat with fileFormat in fileFormats
		if contents of fileFormat is "PDF" then
			export front document to POSIX file (targetFilePath & ".pdf") as PDF
		end if
		if contents of fileFormat is "Microsoft Excel" then
			export front document to POSIX file (targetFilePath & ".xlsx") as Microsoft Excel
		end if
		-- This one will create a folder with csv's if > 1 sheet
		if contents of fileFormat is "CSV" then
			export front document to POSIX file (targetFilePath & ".csv") as CSV
		end if
		(* This one is not scriptable with AppleScript
		if contents of fileFormat is "TSV" then
			export front document to file (targetFilePath & ".tsv") as TSV
		end if
		*)
		if contents of fileFormat is "Numbers 09" then
			export front document to POSIX file (targetFilePath & ".numbers") as Numbers 09
		end if
	end repeat
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.