CSV-Import to Numbers 09 - problem with clear a table before insert

Hello,

my script opens a csv-file (;-separated) in Numbers 09, and transfers the complete content via the clipboard to a particular table inside a second table. Before inserting I want to delete the content inside the “target”-table.

This wont work, I always get an error:

class cannot changed to type reference

Without the line “clear range range …” the script runs fine.

I have no idea how I can solve the problem. Hope you can help me, thanks in advance!

Nils

This is my script:


--define for both files path and name
set importFile to ((path to desktop) & "export.csv" as string) as alias
set evalFile to ((path to desktop) & "evaluation.numbers" as string) as alias
tell application "Finder"
	--change file extension for the csv to "numbers" because Numbers gives a *.csv *.numbers as filename
	set importFileName to (text 1 thru (offset of "." in (get name of (info for importFile))) of (get name of (info for importFile))) & "numbers"
	set evalFileName to (get name of (info for evalFile))
end tell


tell application "Numbers"
	--open both files, copy all from the csv-file, close the csv-file
	open evalFile
	open importFile
	set myActiveTable to the first table of sheet 1 of document importFileName
	tell myActiveTable
		set the rangeStart to the name of cell 1 of column 1
		set the rangeEnd to the name of last cell of last column
		set selection range to range (rangeStart & ":" & rangeEnd)
		activate
		tell application "System Events"
			keystroke "c" using {command down}
			keystroke return
		end tell
	end tell
	close document importFileName without saving
	--clear Table-2 in the second file, insert from the clipboard
	set myActiveTable to the table "Table-2" of sheet "Sheet-2" of document evalFileName
	tell myActiveTable
		set the rangeStart to the name of cell 1 of column 1 as string
		set the rangeEnd to the name of last cell of last column as string
		--the next gives always an error: class <correct range> cannot changed to type reference
		clear range range (rangeStart & ":" & rangeEnd)
		set selection range to range "A1"
		activate
		tell application "System Events"
			keystroke "v" using {command down}
			keystroke return
		end tell
	end tell
end tell
tell application "Finder"
	activate
	display dialog "Finished!"
end tell
tell application "Numbers"
	activate
end tell

Model: MacBook Late 2007
AppleScript: 2.1.2
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Hi, Rebewslin. Welcome to MacScripter.

There should only be one “range” in that line:

clear range (rangeStart & “:” & rangeEnd)

The command verb’s clear and the rest is the range specifier.

Hello Nigel,

thanks for your really fast answer! It works! Thank you! Seems that I have not see the wood for the trees :wink:

I have another problem with this script. Because I use GUI-Scripting (CMD+v) for the insert sometimes the script uses the wrong table and insert there a new table … That’s not what I want.

With 2 Sheets (as in my example) it works. But I want to use it with a larger table with 5 Sheets/ 5 and more tables each. My hope is, that it is possible to use “native” scripting, without GUI-scripting. Something like this:


tell myActiveTable
		set the rangeStart to the name of cell 1 of column 1 as string
		set the rangeEnd to the name of last cell of last column as string
		clear range (rangeStart & ":" & rangeEnd)
		set the value of the range (rangeStart & ":" & rangeEnd) to the clipboard
	end tell

But it wont work, AS gives me an error:

žvalue of range" cannot set as žanything". " number -10006 from value of range to anything

What is the syntax to insert content from the clipboard to a different range (not only to one cell)?

Nils

Hi, Nils.

Annoyingly, Numbers doesn’t let you set the value of an entire range, nor does it have a paste command. You can either set the cell values individually (which is slow but sure) or you can set the selection range of the table you’re targeting to the range you want to fill and use GUI scripting to paste the data. I haven’t tried with multiple sheets, but with multiple tables, setting a selection range brings the table concerned into “focus”.

That part of the code would look something like this:

-- tell application "Numbers"
	-- activate
	tell myActiveTable
		set the rangeStart to the name of cell 1 of column 1 as string
		set the rangeEnd to the name of last cell of last column as string
		set targetRange to range (rangeStart & ":" & rangeEnd)
		clear targetRange
		set selection range to targetRange
	end tell
-- end tell
-- Numbers's "Paste and Match Style":
tell application "System Events" to keystroke "v" using {command down, shift down, option down}

Hi Nigel,

thanks again. It works but only partially. At first I’ve made little corrections to your code, before I always get an error -10000 from AS.

Now I’m using this code:


...
tell myActiveTable
		set the rangeStart to the name of cell 1 of column 1 as string
		set the rangeEnd to the name of last cell of last column as string
		set targetRange to (rangeStart & ":" & rangeEnd)
		clear range targetRange
		set selection range to range "A1"
	end tell
end tell

--Numbers's "Paste and Match Style":
tell application "System Events" to keystroke "v" using {command down, shift down, option down}

I’ve corrected the “Insert”-Range to “A1”. Before (with the complete range) Numbers inserts only content with the size of this range. Because the content from my CSV-file is different, I’m using “A1” now. This adapt the “INSERT”-Range dynamically to the content from the clipboard.

One problem remains:

If my evaluation-table was saved with the cursor inside a different table than the “INSERT”-table, the script runs, cleares the range and pointed the cursor to “A1” (both inside the proper table). But the GUI-Scripting inserts from the clipboard always to the cursor-position from the last file-save and -in this case- not to the proper table. Thats ugly …

Seems that the “selection range” cannot “point” to the proper table for GUI-Scripting. Now I’m looking for a way to set the proper table for access via GUI-scripting …

Nils

The given code failed to define what was at front.

Here is one using one of my predefined handlers.

my activateGUIscripting()
set the clipboard to "just for see"

tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	set the rangeStart to the name of first cell
	set the rangeEnd to the name of last cell
	clear range (rangeStart & ":" & rangeEnd)
	set selection range to range "c2"
end tell
my shortcut("Numbers", "v", "cas")
--=====

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 ==== 
*)
(*
This handler may be used to 'type' text, invisible characters if the third parameter is an empty string. 
It may be used to 'type' keyboard shortcuts if the third parameter describe the required modifier keys. 
*)
on shortcut(a, t, d)
	local k
	tell application a to activate
	tell application "System Events" to tell application process a
		set frontmost to true
		try
			t * 1
			if d is "" then
				key code t
			else if d is "c" then
				key code t using {command down}
			else if d is "a" then
				key code t using {option down}
			else if d is "k" then
				key code t using {control down}
			else if d is "s" then
				key code t using {shift down}
			else if d is in {"ac", "ca"} then
				key code t using {command down, option down}
			else if d is in {"as", "sa"} then
				key code t using {shift down, option down}
			else if d is in {"sc", "cs"} then
				key code t using {command down, shift down}
			else if d is in {"kc", "ck"} then
				key code t using {command down, control down}
			else if d is in {"ks", "sk"} then
				key code t using {shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "k" then
				key code t using {command down, shift down, control down}
			else if (d contains "c") and (d contains "s") and d contains "a" then
				key code t using {command down, shift down, option down}
			end if
		on error
			repeat with k in t
				if d is "" then
					keystroke (k as text)
				else if d is "c" then
					keystroke (k as text) using {command down}
				else if d is "a" then
					keystroke k using {option down}
				else if d is "k" then
					keystroke (k as text) using {control down}
				else if d is "s" then
					keystroke k using {shift down}
				else if d is in {"ac", "ca"} then
					keystroke (k as text) using {command down, option down}
				else if d is in {"as", "sa"} then
					keystroke (k as text) using {shift 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 is in {"ks", "sk"} then
					keystroke (k as text) using {shift 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}
				else if (d contains "c") and (d contains "s") and d contains "a" then
					keystroke (k as text) using {command down, shift down, option down}
				end if
			end repeat
		end try
	end tell
end shortcut

--=====

Yvan KOENIG (VALLAURIS, France) lundi 19 avril 2010 11:32:07

Hi, Nils.

On both my machines, with Numbers 2.0.3, if the cursor’s in a different table in the same sheet, setting the selection range in the desired table brings that table into “focus” ” ie. ready to accept keystrokes and pasting. However, if the wrong sheet is to the fore, that’s difficult. I’ve worked out a GUI solution which works on both my machines (with my test document), but of course it’s not guaranteed to work for anyone else. :wink:

-- Say you want to paste into table 1 of sheet 2 of the front document.

-- tell application "Numbers" to activate

-- Switch the focus to the sheet "Sheet 2" in the front document.
tell application "System Events"
	tell application process "Numbers"
		try
			-- This works with Numbers 2.0.3 on my Tiger machine.
			set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of static texts is {"Sheet 2"}) to true
		on error number -1719
			-- This works with Numbers 2.0.3 on my Snow Leopard machine.
			set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of groups's static texts is {{"Sheet 2"}}) to true
		end try
	end tell
end tell

-- Clear table 1 of sheet 2 and set the selection range.
tell application "Numbers"
	tell table 1 of sheet 2 of document 1
		clear cell range
		set selection range to range "A1"
	end tell
end tell

-- Paste in the clipboard contents.
tell application "System Events" to keystroke "v" using {command down, option down, shift down}

Yep, that is (was!) the problem! Thank you Nigel for the solution! Thank you also to Yvan. Your post can’t solve the problem. But-many of your Numbers 09-scripts (on your Mobile Me-site) helped me to learn scripting numbers!

I have expanded Nigels solution to a handler, useful to change the sheet in the focus. This is the handler and an example for a call of this:


--call
set myactiveSheet to "Sheet-1"
ChangeSheetinFocus(myactiveSheet)


--handler
on ChangeSheetinFocus(mySheet)
	tell application "Numbers"
		activate
		tell application "System Events"
			tell application process "Numbers"
				try
					-- This works with Numbers 2.0.3 on my Tiger machine.
					set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of static texts is {mySheet}) to true
				on error number -1719
					-- This works with Numbers 2.0.3 on my Snow Leopard machine.
					set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of groups's static texts is {{mySheet}}) to true
				end try
			end tell
		end tell
	end tell
end ChangeSheetinFocus


Thank you guys, see you next on macscripter.net!

Nils

Here is a slightly modified version.
It doesn’t use try but call directly the code required by the in use OS.


set myactiveSheet to "Sheet 2"
ChangeSheetinFocus(myactiveSheet)

on ChangeSheetinFocus(mySheet)
	set itIsOs4 to 5 > (system attribute "sys2")
	tell application "Numbers" to activate
	if itIsOs4 then
		tell application "System Events" to tell application process "Numbers" to set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of static texts is {mySheet}) to true
	else
		tell application "System Events" to tell application process "Numbers" to set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of groups's static texts is {{mySheet}}) to true
	end if
end ChangeSheetinFocus

Yvan KOENIG (VALLAURIS, France) mercredi 21 avril 2010 10:51:03

I wish to say that the given script is not safe.

Just an example.

First sheet is named “Sheet 1”
and contains a table named “main”.
This sheet is ‘open’ so the name of the table “main” is displayed

Second sheet is named as you want
and contains a table named as you want.
The focus is on this sheet.

Third sheet is named “main”
and contains a table named as you want.
This sheet is ‘closed’ so we don’t see the table’s name.

Asking the script to trigger the sheet “main” will in fact trigger the table “main”.

Happily, I was able to build a script taking care of that under 10.5 and higher
but at this time I have no soluce under 10.4.11

Maybe somebody will be able to fill the gap.



my selectSheet("main", 1)

--=====

on selectSheet(sName, dName)
	tell application "Numbers" to activate
	tell application "System Events" to tell application process "Numbers"
		if (system attribute "sys2") < 5 then
			(* This works with Numbers 2.0.3 on my Tiger machine
				if there is no table with the same name than the target sheet
				in an higher row *)
			set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window dName whose value of static texts is {sName}) to true
		else
			(* This works with Numbers 2.0.3 on my Snow Leopard machine
				even if there is a table with the same name than the target sheet
				in an higher row.
				It's also able to 'open' the sheet by clicking its black triangle*)
			tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window dName
				repeat with i from 1 to (count of rows)
					set issName to (class of UI element 1 of row i is group) and (get value of static text 1 of group 1 of row i) is sName
					if issName then exit repeat
				end repeat
				if issName then
					select row i
					click UI element 1 of group 1 of row i
				end if -- issName
			end tell -- outline.
		end if -- (system attribute.
	end tell -- System Events .
end selectSheet

Yvan KOENIG (VALLAURIS, France) vendredi 23 avril 2010 21:15:51

Bingo.

I was able to fill an important part of the gap.
Now, under 10.4.11, the script select a sheet even if a table has the same name.
The remaining gap is :
is it a way to ‘open’ a sheet which isn’t ?
The script is able to check this status.
It calculates the location of the black triangle in front of the sheet’s name but trying to click it changes nothing.
What am’I doing wrong ?



my ChangeSheetinFocus(1, "main")

--=====

on ChangeSheetinFocus(dName, sName)
	script myScript
		property lesFeuilles : {}
		local itIsOs4, ismySheet
		tell application "Numbers"
			activate
			tell document dName to set my lesFeuilles to name of sheets
		end tell -- Numbers
		
		if sName is not in my lesFeuilles then
			set my lesFeuilles to {}
			if my parleAnglais() then
				error "The sheet "" & sName & "" is unavailable in the spreadsheet "" & d & "" !"
			else
				error "La feuille « " & sName & " » n'existe pas dans le tableur « " & d & " » ! "
			end if -- my parleAnglais
		end if -- mySheet
		
		set itIsOs4 to 5 > (system attribute "sys2")
		tell application "Numbers" to activate
		tell application "System Events" to tell application process "Numbers" to tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window dName
			if itIsOs4 then
				set sheetSize to get size of row 1
				repeat with i from 1 to (count of rows)
					set issName to (size of row i = sheetSize) and (get value of static text 1 of row i) is sName
					if issName then
						select row i
						try
							set nextSize to size of row (i + 1)
						on error
							set nextSize to sheetSize
						end try
						if nextSize = sheetSize then
							set {iX, iY} to (get position of row i)
							set blackTriangle to {iX + 6, iY + 11} (* position of the black triangle *)
							click at blackTriangle (* Alas, it doesn't open the sheet *)
						end if -- sh
						exit repeat
					end if
				end repeat
			else
				repeat with i from 1 to (count of rows)
					set issName to (class of UI element 1 of row i is group) and (get value of static text 1 of group 1 of row i) is sName
					if issName then
						select row i
						click UI element 1 of group 1 of row i
						exit repeat
					end if
				end repeat
			end if -- (system attribute.
		end tell -- System Events .
		set my lesFeuilles to {}
	end script
	run myScript
end ChangeSheetinFocus

--=====

on parleAnglais()
	local z
	try
		tell application "Numbers" to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

Yvan KOENIG (VALLAURIS, France) samedi 24 avril 2010 19:21:31

Hi, Yvan.

I’m not experiencing this problem on my 10.6.3 machine (using your version of the script in post #9). The sheet “main” is selected every time, even though it comes below the table “main” in the Sheets list. I suspect it may be the nested structure of the UI elements which makes it work properly, but I haven’t looked into that yet.

On my 10.4.11 machine, it’s the table “main” that’s selected, as you describe. I’ve managed to the identify the sheet row on that system by taking the “AXDisclosureLevel” attribute into account too. It’s value appears to be 0 for sheets and 1 for tables. I hope this works for you too.


set myactiveSheet to "main"
ChangeSheetinFocus(myactiveSheet)

on ChangeSheetinFocus(mySheet)
	set itIsOs4 to 5 > (system attribute "sys2")
	tell application "Numbers" to activate
	if itIsOs4 then
		tell application "System Events" to tell application process "Numbers" to set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose (value of attribute "AXDisclosureLevel" is 0) and (value of static texts is {mySheet})) to true
	else
		tell application "System Events" to tell application process "Numbers" to set value of attribute "AXSelected" of (first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose value of groups's static texts is {{mySheet}}) to true
	end if
end ChangeSheetinFocus

With regard to your query about “opening” a sheet (ie. disclosing the names of its tables in the Sheets column), the rows in both 10.4.11 and 10.6.3 have an “AXDisclosing” attribute whose value can be set to true or false to disclose or hide the table names.

Edit: I suspected above that it might be the structure which made the script immune in OS 10.6.3 to the problem Yvan described. I’ve now checked and it is. “Table” rows still have a static text element, like all the rows in OS 10.4.11, while “sheet” rows in OS 10.6 3 have a group element and it’s this which has the static text, so there’s no chance of a “sheet” filter picking up a “table” row.

Instead of writing ‘whose value of groups’s static texts is {{mySheet}}’, it might be clearer to phrase it as ‘whose value of first group’s first static text is mySheet’. (My fault, originally.) The complete script might then look something like this:


set myactiveSheet to "main"
ChangeSheetinFocus(myactiveSheet)

on ChangeSheetinFocus(mySheet)
	set itIsOs4 to 5 > (system attribute "sys2")
	tell application "Numbers" to activate
	tell application "System Events" to tell application process "Numbers"
		if itIsOs4 then
			set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose (value of attribute "AXDisclosureLevel" is 0) and (value of first static text is mySheet)
		else
			set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 whose (value of first group's first static text is mySheet)
		end if
		tell targetSheetRow to set {value of attribute "AXSelected", value of attribute "AXDisclosing"} to {true, true}
	end tell
end ChangeSheetinFocus

Hello Nigel

I saw your response.
I’m too tired to make tests now.
I will do that tomorrow.

Yvan KOENIG

Hi, Yvan.

I saw your post in AS-Users about things not working when the target sheet contains no tables. The same is true for the above script. I’ll try and fix it tomorrow.

OK. This works for me:


set myactiveSheet to "main"
ChangeSheetinFocus(myactiveSheet)

on ChangeSheetinFocus(mySheet)
	set itIsOs4 to 5 > (system attribute "sys2")
	tell application "Numbers" to activate
	tell application "System Events" to tell application process "Numbers"
		if itIsOs4 then
			-- '(value of attributes contains 0)': '(value of attribute "AXDisclosureLevel" is 0)' sometimes works in Tiger, sometimes not. The only possible instances of 0 amongst the attributes are the disclosure level of a sheet row and the index of the first row, which represents a sheet anyway. Another possibility is '(value of attribute -1 is 0)', which makes me uneasy.
			set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 where ((value of attributes contains 0) and (value of first static text is mySheet))
		else
			set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 where ((value of attribute "AXDisclosureLevel" is 0) and ((groups is {}) and (value of first static text is mySheet)) or (value of first group's first static text is mySheet))
		end if
		tell targetSheetRow to set {value of attribute "AXSelected", value of attribute "AXDisclosing"} to {true, true}
	end tell
end ChangeSheetinFocus

Thanks a lot.

After fighting against the odd effects of a test with a wrong tool I was at last ready to test your script when I discovered the late one.

I can’t test it under 10.6 because the imac is elsewhere with my daughter.
I tested it under 10.4.11.
With every case it behaves flawlessly.
So, I’m glad to be able to write:
Problem solved !
Thank you Nigel.

I let you post your fine script in the other forum.
Maybe some one would be interested.

Now, I will try to add some instructions to select a table in a sheet.

Yvan KOENIG (VALLAURIS, France) lundi 26 avril 2010 11:48:57

Here are the final handlers :


--{code}
set dName to 1 (* I pass the true docName when I switch between different documents *)
set myactiveSheet to "main_1"
set myActiveTable to "Tableau 3"

my selectSheet(dName, myactiveSheet)
my selectTable(dName, myactiveSheet, myActiveTable)

--=====

on selectSheet(theDoc, theSheet)
	script myScript
		property listeObjets : {}
		local maybe, targetSheetRow
		tell application "Numbers"
			activate
			set theDoc to name of document theDoc (* useful if the passed value is a number *)
			tell document theDoc to set my listeObjets to name of sheets
		end tell -- Numbers
		
		set maybe to theSheet is in my listeObjets
		set my listeObjets to {} -- So it will not be saved in the script *)
		if not maybe then
			if my parleAnglais() then
				error "The sheet "" & theSheet & "" is unavailable in the spreadshhet "" & d & "" !"
			else
				error "La feuille « " & theSheet & " » n'existe pas dans le tableur « " & d & " » ! "
			end if -- my parleAnglais
		end if -- not maybe
		
		set maybe to 5 > (system attribute "sys2")
		tell application "System Events" to tell application process "Numbers"
			if maybe then (* macOS X 10.4.x
'(value of attributes contains 0)': '(value of attribute "AXDisclosureLevel" is 0)' sometimes works in Tiger, sometimes not.
The only possible instances of 0 amongst the attributes are the disclosure level of a sheet row and the index of the first row, which represents a sheet anyway.
Another possibility is '(value of attribute -1 is 0)', which makes me uneasy. *)
				set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc where ((value of attributes contains 0) and (value of first static text is theSheet))
			else (* macOS X 10.5.x or higher *)
				set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc where ((value of attribute "AXDisclosureLevel" is 0) and ((groups is {}) and (value of first static text is theSheet)) or (value of first group's first static text is theSheet))
			end if -- maybe.
			
			tell targetSheetRow to set {value of attribute "AXSelected", value of attribute "AXDisclosing"} to {true, true}
			
		end tell -- System Events
	end script
	run myScript
end selectSheet

--=====

on selectTable(theDoc, theSheet, theTable)
	script myScript
		property listeObjets : {}
		local maybe, targetSheetRow, rowIndex, r
		tell application "Numbers"
			activate
			set theDoc to name of document theDoc (* useful if the passed value is a number *)
			tell document theDoc to set my listeObjets to name of sheets
		end tell -- Numbers
		
		set maybe to theSheet is in my listeObjets
		set my listeObjets to {} -- So it will not be saved in the script *)
		if not maybe then
			set my listeObjets to {}
			if my parleAnglais() then
				error "The sheet "" & theSheet & "" is unavailable in the spreadshhet "" & d & "" !"
			else
				error "La feuille « " & theSheet & " » n'existe pas dans le tableur « " & d & " » ! "
			end if -- my parleAnglais
		end if -- not maybe
		
		tell application "Numbers" to tell document theDoc to tell sheet theSheet to set my listeObjets to name of tables
		
		set maybe to theTable is in my listeObjets
		set my listeObjets to {} -- So it will not be saved in the script *)
		if not maybe then
			if my parleAnglais() then
				error "The table "" & theTable & "" is unavailable in the sheet "" & theSheet & ""  of the spreadshhet "" & d & "" !"
			else
				error "La table « " & theTable & " » n'existe pas dans la feuille « " & theSheet & " »  du tableur « " & d & " » ! "
			end if -- my parleAnglais
		end if -- not maybe
		
		set maybe to 5 > (system attribute "sys2")
		
		tell application "System Events" to tell application process "Numbers"
			if maybe then (* macOS X 10.4.x
'(value of attributes contains 0)': '(value of attribute "AXDisclosureLevel" is 0)' sometimes works in Tiger, sometimes not.
The only possible instances of 0 amongst the attributes are the disclosure level of a sheet row and the index of the first row, which represents a sheet anyway.
Another possibility is '(value of attribute -1 is 0)', which makes me uneasy. *)
				set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc where ((value of attributes contains 0) and (value of first static text is theSheet))
			else (* macOS X 10.5.x or higher *)
				set targetSheetRow to first row of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc where ((value of attribute "AXDisclosureLevel" is 0) and ((groups is {}) and (value of first static text is theSheet)) or (value of first group's first static text is theSheet))
			end if -- maybe
			tell targetSheetRow to set {value of attribute "AXSelected", value of attribute "AXDisclosing"} to {true, true}
			(*
Awful trick to get the row index *)
			try
				targetSheetRow as text (* to issue an error *)
			on error errMsg (*
(errMsg as text) is :
*Impossible de transformer «class crow» 4 of «class outl» 1 of «class scra» 1 of «class splg» 1 of «class splg» 1 of window "attributs.numbers" of «class pcap» "Numbers" of application "System Events" en type string.*)
				set rowIndex to item 1 of my decoupe(item 2 of my decoupe(errMsg as text, "» "), space)
			end try
			tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc
				repeat with r from rowIndex to count of rows
					if value of first static text of row r is theTable then
						select row r
						exit repeat
					end if -- value.
				end repeat
			end tell -- outline 1 .
		end tell -- System Events
	end script
	run myScript
end selectTable

--=====

on parleAnglais()
	local z
	try
		tell application "Numbers" to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

on decoupe(t, d)
	local l
	set AppleScript's text item delimiters to d
	set l to text items of t
	set AppleScript's text item delimiters to ""
	return l
end decoupe

--=====
--{code}

Yvan KOENIG (VALLAURIS, France) lundi 26 avril 2010 21:17:35

Hi, Yvan.

targetSheetRow’s index (0-based) can be read directly from another of its attributes:


-- (Part of the selectTable handler.)

tell application "System Events" to tell application process "Numbers"
	-- Identify the target sheet row, tnen:
	
	tell targetSheetRow to set value of attribute "AXDisclosing" to true
	-- Get the sheet row's 0-based index + 2 for the following row's 1-based index.
	set r to (value of attribute "AXIndex" of targetSheetRow) + 2
	tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc
		repeat until (value of first static text of row r is theTable)
			set r to r + 1
		end repeat
		set value of attribute "AXSelected" of row r to true
	end tell -- outline 1 .
end tell -- System Events

Son of a . my eyes where wide shut.
I listed every attributes properties and missed that .
And this morning it’s not better. I activated the US keyboard :frowning:

Yvan KOENIG (VALLAURIS, France) mardi 27 avril 2010 09:31:51

PS. I understand why I missed it.

I looked attributes of row 1.
As i saw only zeroes, I didn’t thought that they may be numbered the Apple way (starting from zero).