Selecting all and deselecting data rows

I have two problems…

When I use -A to sellect all rows in a data source and then try and process the “selected rows” I get an error. If I select all the rows with a shift-mouse select then all works fine. Why are these two things different, and how can I fix that?

When I process the selected rows I also remove the row from my list. This works fine, however I don’t know how to tell applescript that the particular row should no longer be selected. What happens is that I end up with a new selection that I didn’t origianally want.

for example. If I select row 4, process it and then delete the data row, row 4 (which is not wat was in row 5) is still selected.
Is there a way to set selected to false?

tdog,

there are 2 possibilities to ask a table view for it’s selected rows:

tell table view "tv" of scroll view "sv" of window "main"
	set sel to selected rows
end tell

gives a list of row numbers (integer) - like this:
{1, 2, 3, 4, 5, 6, 7, 8, 9}

tell table view "tv" of scroll view "sv" of window "main"
	set sel to selected data rows
end tell

gives a list of references to the tv’s data source: like this:
{data row id 7 of data source id 4, data row id 8 of data source id 4, data row id 9 of data source id 4, data row id 10 of data source id 4, data row id 11 of data source id 4, data row id 12 of data source id 4, data row id 13 of data source id 4, data row id 14 of data source id 4, data row id 15 of data source id 4}

in both cases it is possible to reset / set the selection by giving it such a list as parameter:

tell table view "tv" of scroll view "sv" of window "main"
	set selected rows to {1,2,3,4,7,8,9}
	set selected data rows to {data row id 7 of data source id 4, data row id 8 of data source id 4, data row id 15 of data source id 4}
	set selected rows to {} -- no selection
	set selected data rows to {} -- no selection
end tell

The big advantage of using ‘data rows’ is, that this references remain after deleting item(s) (while when deleting a row of a table view any higher row number will be decremented by 1) - So if you for example want to delete a single item of the selected rows and want the rest to remain selected you could do it like so:

	tell table view "tv" of scroll view "sv" of window "main"
		set sel to selected data rows
		tell data source
			delete item 3 of sel
		end tell
		set selected data rows to ((items 1 thru 2 of sel) & (items 4 thru -1 of sel))
	end tell

In no case there should be a difference between a ‘Cmd-A’- and a clicked selection - maybe you could post some code?

Hope that helps …

D.

I am having a similar problem and am frankly a bit baffled. I’ve tried everything for nearly 3 hours here. I just want to set the selected rows of a table to the row whose data cell 5 = a key value.

Here’s the most concise of what I’ve tried:


tell table view thetable of scroll view thescroll of tab view item "results" of tab view "maintabs" of window "Main"
set the_row to (item 1 of ((every data row of theDataSource) whose (contents of data cell 5) is equal to theinfo))
my logit(1, the_row as text)
set selected data rows to {the_row}
end tell

From the internal log it is clear that the_row is in the expected form (even though it can’t make it into a string):
[11/29/07 2:43:34 AM] Can’t make «class datR» id 168 of «class datS» id 94 into type string.

And yet it simply won’t select the row. (even when commenting out the log error).

Ive tried various other approaches (repeat loops, row instead of data row, etc) based on this post but nothing seems to work. I should note that this table is connected to a “selection changed” handler too. I cannot remember right now if I tried turning that off but I think I did. In any case that is definitely needed and for other reasons it is heavily flagged so it actually does nothing at the point that this selection is supposed to be made.

Thanks

EDIT:
I think I resolved this problem by just taking out the “selection changed” handler which was causing other problems too. Boy that’s a grabby one! However, I must say that after making several applications with tables, I never seem to be able to settle on one system that works – it’s is a grab bag every time.

It’s never clear to me whether you have to refer to data sources within tell blocks to the table view or not. Sometimes it seems to be necesssary, other times it seems to cause errors.

And if I could figure out a way to make sure tables could be handled when they are on non-current tab views, that would be swell. Early on in AS Studio it simply never worked, then at least since Tiger it seems to work most of the time but not always. Also – I always have to save the NIB in IB with the tab view item that has the table on it selected. Otherwise I get “can’t make… reference” errors every time.