Some help with a data source for a table

I have a table and a data source that have one column “title”. I want this column to be sorted and when my app opens I want to select the first row of the table. Here is my code:

property dataSource : null

on will open theObject
	--set up data source and make a new column for the titles
	set dataSource to data source of table view "titleList" of scroll view "titleList" of theObject
	make new data column at the end of data columns of dataSource with properties ¬
		{name:"title", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}

	--this line executes a shell script in the getValue function that gets a return-delimited list of all the titles
	set theTitles to paragraphs of (getValue of {"long_title"} for "all")

	--make a new row for each title
	--note: the titles are not added in alphabetical order
	repeat with theTitle in theTitles
		set theRow to make new data row at end of data rows of dataSource
		tell theObject to set contents of data cell "title" of theRow to theTitle
	end repeat

	--sorts the data source, in theory
	set sorted of dataSource to true
	set sort column of dataSource to data column "title" of dataSource

	--selects first row of the data source and makes an empty selection impossible
	set selected data row of table view "titleList" of scroll view "titleList" of theObject to first data row of dataSource
	set allows empty selection of table view "titleList" of scroll view "titleList" of theObject to false
end will open

All that look good? But it doesn’t work! When my app opens the table view IS sorted, but the row selected is not the first. It selects the first row I added to the data source, not the first in the table view (which is the first alphabetically). Can anyone help me with this? I’m so frustrated with ASS, I’m this close to just trashing my whole project and going to learn Obj-C.

:slight_smile: That’s funny. If you’re frustrated with applescript, you’ll need some good medication if you’re going to try to learn obj-c instead. I’ve dabbled in both, and applescript is 1.42 million times let stressful to deal with in my opinion. ASS is quite easy to understand, once you learn some of it’s intricacies and get familiar with the object hierachy. Just be patient, and keep asking questions… you’ll get it. :cool:

As you’ve noted, your data source keeps track of only the original list, and does not reorganize itself when the sort order changes. Despite your frustration, this is actually desirable behavior, because otherwise there’d be no way to reference an item in the list once a user changed the sort order. So, setting the selected row based on the data source version of the list is not going to work for you. If you read apple’s docs regarding table views, you’ll find that the tableview class provides two properties related to selecting rows, ‘selected data row’ and ‘selected row’. Fortunately, the selected row property allows you to easily select a table row, without ever mentioning the data source.

set selected row of table view "titleList" of scroll view "titleList" of theObject to 1

If that doesn’t do it for you, let us know, because there will almost always be another way… and lots of us are here willing to help. Before getting terribly upset about the language, spend some time learning it by at least reading through the documentation related to the object you’re having a problem with. There is little that you can’t do with applescript studio that you CAN do with obj-c in many cases. But applescript is SOOO much friendlier.

Keep the faith… :wink:

Thanks very much for the reply. I do like AppleScript a lot but it’s annoying when things don’t work how you expect and the documentation is not very good. I’m mainly a Java programmer and I despise C, but I need a good aqua GUI for my Mac apps so I’m really trying to make AppleScript work. There are tons of other problems I’m having besides this one, but we’ll go one at a time :slight_smile:

Okay, I tried what you said but it still doesn’t work how I want. I now realize why it isn’t working thanks to your post, but I still can’t figure out how to fix it :expressionless:

The problem is that the correct row is being selected in the table, but the corresponding data is not being loaded. For example, row 1 is selected but the data for row 3 is showing up in my text fields. The reason for this is because before the column was sorted, row 3 was row 1. Here is the code for my selection changed event:

on selection changed theObject
	if the name of theObject is "titleList" then
		set theRow to selected data row of theObject --here is the problem, the data row != the row
		tell window of theObject to set theTitle to contents of data cell "title" of theRow

		--again, the getValue function is just fetching the data from the database
		set titleData to getValue of {"author", "summary", "isbn"} for theTitle

		set contents of text field "author" of window of theObject to paragraph 1 of titleData
		set contents of text view "summary" of scroll view "summary" of window of theObject to paragraph 2 of titleData
		set contents of text field "isbn" of window of theObject to paragraph 3 of titleData
	end if
end selection changed

Is there a way I can have the data for the selected row of theObject be loaded?

Sorry in advance if I’m jumping the gun here, but I believe the solution to my problem would be this:

set selected data row of table view "titleList" of scroll view "titleList" of theObject to (item 1 of sorted data rows of dataSource)

In theory, this should work because sorted data rows of dataSource is a list of the data rows in their sorted order and item one should also be the first row in the sorted table.

However, sorted data rows is YET ANOTHER “feature” of Applescript Studio that just flat out doesn’t work. It’s documented. It’s supposed to be part of the language, but it just doesn’t work. It returns nothing. It’s just like fonts in text views and table views. Why can’t you set the font of a text view? It’s a documented feature of the language. It’s in the release notes. But it does nothing. Does Apple just put this crap in their documentation without ever confirming with the dev team that it’s actually IN THE LANGUAGE?

Okay, sorry, maybe I’m still doing something wrong. But I’m pretty sure that one line of code would work, if sorted data rows actually did what it was supposed to do. Is there another solution to this problem?

Thank you for the response. I just got really frustrated dealing with the data source so I removed it altogether. Whenever I was calling get sorted data rows on the data source it was returning null, and no matter what I did I couldn’t get it to select the first row of the table but an arbitrary row of the data source (since the rows didn’t match after sorting).

Now I’m just keep track of my own data and using the cell value handler to pass it to the table view. I can also use the shell command sort to do all the sorting I need. It’s very flexible and powerful and a lot more intuitive than using a data source, in my opinion.