Printing a sheet from Numbers to PDF

Can anyone help?

In Numbers this is easy “⌘p/PDF/Save as PDF…”

I’d like to be able to do the same in AppleScript but are having very little luck. Using System events and clicking the PDF menu button is proving very elusive. The popup window after the ⌘p doesn’t appear to have a name if you explore the object and so far all attempt to get this apparently simple task to work have failed.

Also tried “print its sheet “Client Invoice” with properties {target printer:“Adobe PDF 8.0”}” but AppleScript complained that “Numbers got an error: sheet “Client Invoice” of document “…” doesn’t understand the print message.”

All help and ideas gratefully received.

Hi, Derek.

On both my systems, Numbers’s “Print” dialog is a ‘sheet’ attached to the Numbers window:

tell application "System Events"
	tell application process "Numbers"
		set frontmost to true
		keystroke "p" using {command down}
		tell window 1
			tell sheet 1 -- "Print" dialog.
				repeat until (it exists)
					delay 0.2
				end repeat
				tell menu button "PDF" -- "PDF" button.
					click
					tell menu item 2 of menu 1 -- Menu item "Save as PDF.".
						repeat until (it exists)
							delay 0.2
						end repeat
						click
					end tell
				end tell
				repeat while (it exists) -- "Print" dialog.
					delay 0.2
				end repeat
				-- When "sheet 1 of window 1" no longer exists, it means that the "Save" dialog is now window 1!
			end tell
			-- Put the rest of your save code here.
		end tell
	end tell
end tell

Thanks for the script. Worked perfectly.

The delays are what makes it work. You have to wait for the page/sheet/dialog to load before proceeding.