Can't set data source for table

Hi,

I’m trying to create a data source for a table. I’ve been working from the studio example Table. I’m using ‘dialogs’ to see just how far this gets (is there a better way to debug this?). When run, the first dialog displays “trackTable”, the second “set achieved” but the third generates the error “The variable trackTableSource is not defined. (-2753)”. I don’t understand this. How did it get past the “set…” line? Any ideas on the error?

Brad

(FYI: 10.4.3 and xcode 2.0)


on will open theObject
	-- Set up the trackTableSource so that the rest will be simpler
	display dialog "trackTable"
	set trackTableSource to data source of table view "trackTable" of scroll view "trackTable" of theObject
	display dialog "set achieved"
	display dialog (class of trackTableSource) as string
	-- Here we will add the data columns to the data source of the track table view
	tell trackTableSource
		--display dialog "inside tell"
		make new data column at the end of the data columns with properties {name:"Trackpoints"}
		make new data column at the end of the data columns with properties {name:"Date"}
		make new data column at the end of the data columns with properties {name:"Start Time"}
		make new data column at the end of the data columns with properties {name:"End Time"}
		make new data column at the end of the data columns with properties {name:"Track Name"}
	end tell
end will open

Dealing with data sources is a tough topic, and can be frustrating and complex. The first obvious problem, is that a data source is an applescript object that you must create. The data source is not automatically created for you, so when you reference “trackTableSource” it’s a missing value… thus it has no class to get. Also, you’ll want to keep track of whether you’ve already created the data source or not, so you don’t create a new data source every time you open the window attached to the will open handler shown here. Try using something like the following instead…

property trackTableSource : ""
on will open theObject
	if trackTableSource is "" then
		log "Creating data source"
		set trackTableSource to make new data source at end of data sources with properties {name:"trackTableSource"}
		log "Setting data columns"
		tell trackTableSource
			make new data column at the end of the data columns with properties {name:"Trackpoints"}
			make new data column at the end of the data columns with properties {name:"Date"}
			make new data column at the end of the data columns with properties {name:"Start Time"}
			make new data column at the end of the data columns with properties {name:"End Time"}
			make new data column at the end of the data columns with properties {name:"Track Name"}
		end tell
		log "Setting data source"
		set data source of table view "trackTable" of scroll view "trackTable" of theObject to trackTableSource
		log "Set data source"
	end if
end will open

Regarding your question about debugging…

I prefer to use the “log” command instead. Dialogs are notorious for screwing up the application’s flow, and for handling some data types incorrectly, making them a nuisance and a pain to coerce values for. Variable types like strings, numbers, and lists are automatically supported so you can simply send them the log command and their value will be output to the Run Log (“Window > Tools > Run Log”). This way you can see what your variables are at any point, without stopping the natural flow of the app.

Good luck,
j

You can either use

set trackTableSource to make new data source at end of data sources with properties {name:"trackTableSource"}

or create a visual one directly in IB and name it.

Thanks.

The log command is easier than display dialog.

Using the two lines:


set trackTableSource to make new data source at end of data sources with properties {name:"trackTableSource"}

and


set data source of table view "trackTable" of scroll view "trackTable" of theObject to trackTableSource

Does indeed make the things function, the data source is created, linked to the table in IB and I can put data in the table. Unfortunately it does not answer the question. The code I’m using (only posted “will open” section for brevity) is essentially based (copied) directly on Apple’s example Table.xcode WithDataSource.applescript. The Apple code does not use ‘make new data source’ anywhere (Table.xcode compiles and runs as expected). As far as I can tell I have the same settings in IB for the table elements (scrollview and tableview), just names that make more sense for my application. In fact while debugging I tried misnaming the IB elements and the code threw an error while running. Ironically at the offending ‘set’ command. Give it valid names and it does nothing. Another one of the great mysteries of Applescript.

It was suggested “or create a visual one directly in IB and name it.” I suspect this is what was done in the Apple example which I thought I’d copied faithfully, unfortunately not quite exactly somehow or it would work. When I click on the table (NSScrollViewInspector) or double click on the table (NSTableViewInspector) in my app and Table.xcode they seem to be configured the same.

Suggestions welcome. Unfortunately I seem to have a need to understand ‘why’ more than getting on with the working code supplied by jobu.

While in Apple’s Table project, take a look at the nib file. In the mainmenu.nib window, you’ll see that there’s a “Contents” ASKDataSource (a blue box) in the “Instances” tab. This is the table’s data source, and as Vincent pointed out, this would be the IB-created version that you are welcome to use instead of doing it programmatically. Doing it this way gets you around creating and connecting it to the table view as I described above. There’s no reason to do it one way vs. the other… only personal preference. Once you’ve created it in IB, then control-drag from the table view to the datasource object and set it as the table view’s data source in the connections tab.

Glad you got it working,
j

Thanks J,

Adding the data source in IB and connecting it to the tableview solved the mystery. Now I understand one more bit of Studio.

Brad