I want to create a AppleScript to print a Numbers 09' table

I am working in Number’s 09’. I have created a sheet with many tables in it. Each table represents a day of a month and each table is setup as a single page (1 Sheet (Month) = 31 Table (Days). I would like a AppleScript that can print a Table that is selected within the Sheet. I want 1 script to to be able to print 1 copy. I want another script to be able to print 2 copies. I would also like the scripts to be, if possible, in the Menu of Numbers. Can someone please show/tell me how to do this?

Model: Imac 27" 3.4Ghz
AppleScript: 2.5
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Download this archive :

https://www.box.com/s/3ojiishyelk2w2xvg2op

Yvan KOENIG (VALLAURIS, France) jeudi 14 mars 2013 20:33:25

Thanks for the file. I have figured out how to run it and get it to print a specific table of a specific sheet. This is great but I am needing to print various tables and want to do so without having to enter a different Sheet Name and Table Name into the Applescript every time I want to print. Can something be done so that when viewing a table I can then print that table without entering anything new into Applescript?

It seems that you looked too fast.
The archive contain two scripts.
print_table_of_sheet behave the way you described but print_table treats the selected area which seems to be what you want.

For both, the way of use is described at their very beginning in French and in English.

Yvan KOENIG (VALLAURIS, France) vendredi 15 mars 2013 10:12:18

Hi Yvan.

With regard to your print_table(s) script, GUI Scripting doesn’t actually need to be enabled for keystrokes and key codes.

I presume the reason you’ve used keystrokes to effect the printing rather Numbers’s own ‘print’ command is because the latter’s slightly faulty. It ignores the ‘target printer’ property in a ‘with properties’ record (unless I’ve been specifying it incorrectly) and instead tries to use the printer specified in the document. In the case of Numbers’s own “Blank” template, the printer’s the generic “Any printer”, which makes Numbers think no printer’s been set up and it throws an error saying so. But the print dialog defaults to the default printer for the user.

The ‘print dialog’ parameter does in act work, so it could be used instead of a keystroke if so wished:

tell application id "com.apple.iWork.Numbers"
	activate
	-- Identify the currently focused table.
	try
		set sourceSheet to first sheet of front document where (first table whose selection range is not missing value) is not missing value
	on error number -1719
		display dialog "No tables are selected in the front document." buttons {"Abort"} default button 1 cancel button 1 with icon stop
	end try
	set sourceTable to first table of sourceSheet whose selection range is not missing value
	-- Select every cell in the table.
	tell sourceTable to set selection range to cell range
end tell

-- Copy the table.
tell application "System Events" to keystroke "c" using command down

-- Open Numbers's "Blank" template and clear it.
tell application id "com.apple.iWork.Numbers"
	open (path to resource "Blank.nmbtemplate" in directory "Templates")
	delete sheets of front document
end tell

-- Paste in the table copied above.
tell application "System Events" to keystroke "v" using command down

tell application id "com.apple.iWork.Numbers"
	-- Select the table for a "focused" print result.
	tell table 1 of sheet 1 of front document to set selection range to cell range
	-- Open the print dialog. Don't wait for a response from it .
	ignoring application responses
		print front document with print dialog
	end ignoring
end tell
-- . but pause for it to appear and then dismiss it.
delay 1
tell application "System Events" to keystroke return

Another alternative, of course, would be to employ a user template that’s set up for the required printer anyway. The print dialog’s then unnecessary:

tell application id "com.apple.iWork.Numbers"
	activate
	-- Identify the currently focused table.
	try
		set sourceSheet to first sheet of front document where (first table whose selection range is not missing value) is not missing value -- !
	on error number -1719
		display dialog "No tables are selected in the front document." buttons {"Abort"} default button 1 cancel button 1 with icon stop
	end try
	set sourceTable to first table of sourceSheet whose selection range is not missing value
	-- Select every cell in the table.
	tell sourceTable to set selection range to cell range
end tell

-- Copy the table.
tell application "System Events" to keystroke "c" using command down

-- Open and clear a user-defined template which has been set up for the required printer.
tell application id "com.apple.iWork.Numbers"
	open file ((path to application support from user domain as text) & "iWork:Numbers:Templates:My Templates:Address labels 63.5 x 38.1 mm 5 A4 sheets of 21.nmbtemplate") -- eg. A template of mine.
	delete sheets of front document
end tell

-- Paste in the table copied above.
tell application "System Events" to keystroke "v" using command down

tell application id "com.apple.iWork.Numbers"
	-- Select the table for a "focused" print result.
	tell table 1 of sheet 1 of front document to set selection range to cell range
	-- Print the table without showing a dialog.
	print front document
end tell

And yes, the ‘whose’ filters used to identify the currently focused table could be combined into one reference: :stuck_out_tongue:

tell application id "com.apple.iWork.Numbers"
	activate
	-- Identify the currently focused table.
	try
		set sourceTable to first table of (first sheet of front document where (first table whose selection range is not missing value) is not missing value) whose selection range is not missing value -- !
	on error number -1719
		display dialog "No tables are selected in the front document." buttons {"Abort"} default button 1 cancel button 1 with icon stop
	end try
end tell

Hello Nigel

I know that in some cases there is no need to explicitely trigger the application process to issue some features.
As I wrote some times ago, I deliberately decided to trigger the process to be sure that I will not forget to do that when it’s required.

The script are old ones so, with recent operating systems, some enhancements may be introduced.
Sometimes, I am lazy and prefer do other things than editing an old beast :wink:

Yvan KOENIG (VALLAURIS, France) vendredi 15 mars 2013 22:28:04