Export or print a Numbers sheet as a PDF file

I have a Numbers document that has many sheets and I regularly need to convert a single sheet to PDF and email it. So I thought I would automate this process. To do this manually I open the print dialog, and configure to print just the sheet and then change the drop down menu to save as PDF.

To date I have had zero success trying to do this using AppleScript. What I have found on sites like maxosxautomation.com seem to be result in the entire document being exported to PDF.

While I wait for my forehead to heal from pounding it against my desk, has anyone done this?


Have you tried putting your AppleScript or an Automator action into the PDF Service folder in your Library folder (a variation on what’s described in this video that shows how to add new locations for saving your pdf to - https://youtu.be/Cc8muVY9XkM )

Numbers should just pass a pdf of what you’ve chosen to print from the print dialogue to either the script or the folder you’ve selected.

No I am not even aware of this folder.

While I will look into this and think about its use I really want this process to be automated so I simply run a function using a keyboard shortcut.

Ok, so it has been awhile but I think I have finally come up with some scripts to do what I set out to do. I would have thought this would have been a great deal easier being able to access commands that exist in Numbers. At this point, an Applescript Noob, I have not figured those steps out. Perhaps in time.

The first step calls the function duplicateNumbersSheet below. It creates a copy of the sheetToDuplicate in theDocument and gives it a name. The handle code is below:

on duplicateNumbersSheet(theDocument, sheetToDuplicate, newSheetName)
	tell application "Numbers"
		tell theDocument
			-- Make sure nothing else is selected, then select all 
			-- and copy all the contents of the template sheet
			set active sheet to sheetToDuplicate
			tell application "System Events"
				-- deselect all
				keystroke "a" using {command down, shift down}
				delay 0.1
				-- select all containers (tables, text items, etc.)
				keystroke "a" using {command down}
				delay 0.1
				-- copy the containers
				keystroke "c" using {command down}
				delay 0.1
			end tell -- System Events
			-- Create the new sheet
			set newSheet to make new sheet with properties {name:newSheetName}
			tell newSheet
				delete every table
				-- Paste in the contents from the template sheet
				tell application "System Events"
					keystroke "v" using {command down}
				end tell --System Events
			end tell
			delay 0.1
		end tell -- theDocument
	end tell --Numbers

Next I save this sheet as a PDF file. This is really the part I find to be less-elegant. The function creates a temporary copy of the Numbers file, deletes all sheets except the desired sheet and then exports the file as a PDF. It then cleans up by removing the temporary numbers file.

Also included below this function is the function duplicateDocumentWithFinder which is used to create the temporary file.

on saveNumbersSheetsAsPDF(currentNumbersDocument, newFileDirectory, newNumbersFileName, theDesiredSheetName)
	set tempDirPOSIX to "/Users/<YourUserNameHere>/Desktop/"
	set newTempFile to (tempDirPOSIX & newNumbersFileName & ".numbers")
	--my duplicateDocumentWithFinder(currentNumbersDocument, newTempFile)
	my duplicateDocumentWithFinder(currentNumbersDocument, newTempFile)
	tell application "Numbers"
		delay 5
		repeat 3 times
				set newDocument to open POSIX file newTempFile
				exit repeat
			on error
				delay 1
			end try
		end repeat
			set numberOfSheets to (count of items in (sheets of newDocument))
		on error
			delay 1
			beep 2
		end try
		set allSheets to (sheets of newDocument)
		-- set remainingSheets to sheetNames
		set listOfSheetData to allSheets as list
		set reverseList to (reverse of listOfSheetData)
		(* Delete all sheets except the sheet we care about *)
		repeat with thisSheetData in reverseList
			--repeat with thisIndex from 1 to numberOfSheets
			--set thisSheet to thisSheetData as sheet
				set nameOfSheet to name of thisSheetData
				if nameOfSheet is not theDesiredSheetName then
					delete thisSheetData
				end if
			on error
				set test to listOfSheets
				display dialog ("nameOfSheet:" & nameOfSheet & "and theDesiredSheetName:" & theDesiredSheetName)
			end try
		end repeat
		save newDocument
			set pdfPOSIXpath to (newFileDirectory & newNumbersFileName & ".pdf")
			export newDocument to POSIX file pdfPOSIXpath as PDF
			delete newDocument
		on error
			set alertString to "Unable to export " & pdfPOSIXpath & "as a PDF"
			display alert alertString buttons {"OK"}
		end try
		return pdfPOSIXpath
	end tell
end saveNumbersSheetsAsPDF

on duplicateDocumentWithFinder(theDocument, theNewDocumentName)
	tell "Finder"
		set sourceFilePath to file of theDocument
		set sourcePOSIXpath to quoted form of the POSIX path of sourceFilePath
		set destinationPOSIXpath to quoted form of the theNewDocumentName
	end tell --Finder
	tell application "Numbers"
		activate theDocument
		set theDocumenetRef to theDocument as specifier
		with timeout of 1200 seconds
			save theDocument in POSIX file theNewDocumentName
		end timeout
	end tell
end duplicateDocumentWithFinder --duplicateNumbersDocument

If I have not explained something sufficiently let me know!