Table scripting example - don't understand one thinig

I am experimenting with the “Table” example script. There are two scripts in there - one with a data source and one without. I have been using the one without a data source. Everything works and the interface makes sense, but I am not seeing how the data being held in the contacts list is tied to the table view. Changes are made to the list (which I see is a list of records, which is pretty cool) but then the script just says tell the table to “update”. How does it know what to update with?? I don’t see anywhere where the list is defined as the data to populate the table.

Is anyone familiar with this example script such that you could explain this?

Thanks.

When your table is linked to a datasource the you just need to say “tell theTable to update” and it knows what the update the table with. But if your table doesn’t have a datasource the you just update it yourself like so…

set content of myTable to myList

That’s what was driving me crazy. There is no line like that in the “no data source” version of the table in the example. I did finally read the comment at the top of the script which says “This script is used to demonstrate the scripting of a table view without using a data source. The important part of supplying the table with information is in the ‘cell value’ and ‘number of rows’ event handlers. The table will query the script asking it for the number of rows, and then for every row of every column the ‘number of rows’ event handler will be called, returning the contents of the cell for the table to display.”

The actual code is:

-- Return the value of the specified column for the given row
on cell value theObject row theRow table column theColumn
	-- Set the value to an empty string for now
	set theValue to ""
	-- Make sure that we aren't being asked for a row that is greater than the number of contacts
	if (count of contacts) ≥ theRow then
		set theContact to item theRow of contacts
		-- Get the identifier of the column so that we can determine which field of the record to return
		set theID to identifier of theColumn
		if the theID is "name" then
			set theValue to name of theContact
		else if theID is "address" then
			set theValue to address of theContact
		else if theID is "city" then
			set theValue to city of theContact
		else if theID is "state" then
			set theValue to state of theContact
		else if theID is "zip" then
			set theValue to zip of theContact
		end if
	end if
	-- Now return the value that we set
	return theValue
end cell value

-- Return the number of contacts
on number of rows theObject
	return count of contacts
end number of rows

So, it’s starting to make sense, but I am still sketchy on WHEN the script runs this “cell value” event that’s tied to the table. Is it just triggered by the “update” command? So, you can basically build your table content however you want in the “cell value” event and then just trigger it with an update whenever you want all the values recalculated?

I hope these aren’t silly questions. I don’t like using code unless I feel like I really understand exactly what it is doing…

Thanks for any additional thoughts/comments/insight you might have.

I don’t understand how it works their way either. That’s why I just update the table myself. I manage my own list, when a button is pressed to add/update/remove something, I change it in my list and then update the table with my new list using “set contents…”. Then I know my list is always up to date and I can do things like search my list or on quitting the application I can save my list so it can be restored at next launch of the application.

When trying to understand their way of managing the table, I put log comments all through each of the handlers so I could tell which handler (and which line of the handler) was being called at what time during a button press. For example, when you press the “add” button here’s what happens…

  1. on_number_of_rows handler is called <----- I do not understand what triggers this and why it is the first handler that gets called
  2. on_clicked handler is called
    a) goes into the “add” section
    b) does the tell window line
    c) does the set contacts line
    d) starts the update table line
    sometime during the update process the on_number_of_rows handler is called <----- why the update process triggers this and not the other handler I do not understand
    e) finishes the update table line
    f) does the clearContactInfo line and handler
    g) leaves the on_clicked handler
  3. enters and leaves the on_cell_value handler 5 times <----- I do not understand what triggers this

So that’s what I see and I do not understand what triggers those certain events. Obviously its the table calling those handlers because that’s what the handlers are connected to, but like you I would have thought that the table would call them during the update process, but it doesn’t call the ‘on cell value’ handler during the update process. The whole process doesn’t even seem to happen as described in the comments at the top of the script. The comments say “for every row of every column the ‘number of rows’ handler is called” buts that’s not what I see.

So bottom line is I do it myself because I don’t fully understand the process as written in that applescript.