Applescript: exporting numbers file to PDF

I have the following code to print a pages file:

[AppleScript]tell application “Pages”
set Doc to open PathFileDoel as alias
set DocPDF to PathFileDoelPDF
export Doc to file DocPDF as PDF
close Doc
end tell
[/AppleScript]

Is there a similar command to export a numbers file to PDF (I suppose so) and can I choose the sheets to export?
The numbers file contains 5 sheets, and I only want to export 3 of them.

Hi,

It is possible, but if you want keep original numbers file as is, you should create temporary duplicate file. Because exporting automatically saves edited numbers document with deleted sheets. And, you should export to existing (pdf) file, to avoid permission problems:


set destinationFolder to (path to desktop) as text
set theFile to choose file of type "numbers"

tell application "Finder"
	set docName to name of theFile
	set theDuplicate to duplicate file (theFile as text) -- create duplicate
	try -- try block, because maybe destination file already exists
		make new file at folder destinationFolder with properties {name:(docName & ".pdf")}
	end try
end tell

tell application "Numbers"
	activate
	set Doc to open (theDuplicate as text as alias) -- open the duplicate 
	delete sheets 4 thru -1 of Doc
	set PDFExportFileName to destinationFolder & docName & ".pdf"
	export Doc to file PDFExportFileName as PDF
	close documents saving no -- quit without saving
end tell

-- delete the temporary duplicate (if need)
tell application "System Events" to delete file (theDuplicate as text)

If the sheets to display contain formulas using data from the other sheets, an extraneous step would be required: copy cells containing formulas and paste the resulting values.
This way you would be able to remove the extraneous sheets without generating errors.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 février 2021 20:42:23

I have done it in the way you suggest it. First make a copy of the file, then copy and past the cells with a formula , delete the unnecessary files, export it to pdf and remove the numbers file.
(Unfortunately, some cells in het workbook are merged cells, so I have the remerge them after the copy past.)

Here is my code (for those who are interested):


on ExportNumbersPDF(Deelverslag, DocumentFullNaam)
	
	tell application "Finder"
		set Map to characters 1 thru -((offset of ":" in (reverse of items of DocumentFullNaam as string)) + 1) of DocumentFullNaam as string
		set Copie to duplicate file DocumentFullNaam to folder Map
		set name of Copie to ((Deelverslag & ".numbers") as text)
	end tell
	--
	tell application "Numbers"
		set Werkboek to open (Map & ":" & Deelverslag & ".numbers") as alias
		tell Werkboek
			if Deelverslag contains "RTO" then
				tell table "RTO-1" of sheet "RTO"
					repeat with i from 14 to 17 by 1
						repeat with j from 2 to 12 by 2
							tell column j
								set celinhoud to value of the cell i
								set value of the cell i to celinhoud
							end tell
						end repeat
					end repeat
				end tell
			end if
			if Deelverslag contains "RTO" or Deelverslag contains "MV" then
				activate
				set BladNaam to Deelverslag & " - Administratieve gegevens"
				tell table "Tabel 1" of sheet BladNaam
					tell column 3
						repeat with i from 5 to 46 by 1
							set celinhoud to value of the cell i
							set value of the cell i to celinhoud
						end repeat
					end tell
				end tell
				set BladNaam to Deelverslag & " - Voorblad"
				tell table "Tabel 1" of sheet BladNaam
					tell column 17
						repeat with i from 1 to 3 by 1
							set celinhoud to value of the cell i
							set value of the cell i to celinhoud
						end repeat
					end tell
					tell column 8
						repeat with i from 6 to 10 by 1
							set celinhoud to value of the cell i
							set value of the cell i to celinhoud
						end repeat
					end tell
					set celinhoud to value of the cell "E24"
					set value of the cell "E24" to celinhoud
					merge range "D24:F24"
					set celinhoud to value of the cell "G24"
					set value of the cell "G24" to celinhoud
					set celinhoud to value of the cell "E31"
					set value of the cell "E31" to celinhoud
					merge range "B31:I31"
					set celinhoud to value of the cell "M31"
					set value of the cell "M31" to celinhoud
					merge range "J31:P31"
				end tell
				
			end if
			repeat with i from (count of sheets) to 1 by -1
				set BladNaam to name of sheet i
				if BladNaam does not contain Deelverslag then
					delete sheet i
				end if
			end repeat
		end tell
		export Werkboek as PDF to file ((Map & ":" & Deelverslag & ".pdf") as text)
		close Werkboek
	end tell
	--
	tell application "Finder" to delete (Map & ":" & Deelverslag & ".numbers")
	
end ExportNumbersPDF


P.S. Why is my code not appearing is the code environment? What am I doing wrong?

Hi Amelie.

The [applescript] and [/applescript] script tags should be lower case only.

Thank you!