Scripting Numbers (09)

There’s conflicting messages out there about Numbers '09, but I have written quite a bit of code which works. What I can’t do however is activate, select, or bring into the window another sheet (in a multisheet document). Anyone say for sure whether it can be done? And if so, how? Thanks in anticipation.

You can try this

activate object worksheet "Sheet2"
	activate object range "A1"
select range "A1"

may be of some help

I read:
(1) activate object worksheet “Sheet2”
object worksheet”, as well as “worksheet” are unavailable in Numbers vocabulary !

(2) activate object range “A1”
object range” is unavailable in Numbers vocabulary !

(3) select range “A1”
select range” is unavailable in Numbers vocabulary !

We may approach what you tried to achieve with:

on run
	my activateGUIscripting()
	set the clipboard to "Yes we can !"
	tell application "Numbers" to tell document 1
		tell sheet "Sheet2" to tell table "Tableau 2"
			set value of cell 4 of column 1 to "I have a dream !"
			set selection range to range "B1"
			my shortcut("Numbers", "v", "c")
		end tell
	end tell
end run


on activateGUIscripting()
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
	end tell
end activateGUIscripting

==== Uses GUIscripting ==== 
on shortcut(a, t, d)
	local k
	tell application a to activate
	tell application "System Events" to tell application process a
		repeat with k in t
			if d is "c" then
				keystroke (k as text) using {command down}
			else if d is in {"ac", "ca"} then
				keystroke (k as text) using {command down, option down}
			else if d is in {"sc", "cs"} then
				keystroke (k as text) using {command down, shift down}
			else if d is in {"kc", "ck"} then
				keystroke (k as text) using {command down, control down}
			else if (d contains "c") and (d contains "s") and d contains "k" then
				keystroke (k as text) using {command down, shift down, control down}
			end if
		end repeat
	end tell
end shortcut


But the table “Tableau 2” of sheet “Sheet2” will not be visible
and even the sheet “Sheet2” will not be activated.
So, we will correctly get “I have a dream !” in cell A4 of Tableau 2 of Sheet1,
the cell B1 of Tableau 2 of Sheet2 will be selected
as neither the sheet nor the table are displayed, the paste instruction apply to the active sheet, in my tests it was Sheet1 and the result is pasted in the cell which was selected in this sheet.

In my iDisk (link available in the thread pointed by Chris2) you may find a more complicated script which, when the running OS is 10.5 or 10.6, may activate a sheet giving us the ability to paste where we want.

I wish to add that paste must be used as a last resort feature.
As I showed in my sample, set value is able to fill a cell even if the sheet is not visible.
More on that, working in a ‘not visible’ table is highly faster than doing the same ins a visible table.

Yvan KOENIG (VALLAURIS, France) samedi 3 octobre 2009 16:21:37

Thanks everyone. Working my way through these…