how do I populate a table from a list? Code not working.

G’day

I’ve just started using Studio, and I’m not sure how to populate a table from a list.

I found most of this on these forums, but running it gives me a ‘variable theSource is not defined’ error after the first two beeps.

Would someone mind pointing out what’s wrong with the script please.

Regards

Santa


	set theSource to data source of table view "Table1" of scroll view "Scroll1" of window "rtfd locator"
	beep 2
	tell theSource
		make new data column at end of data columns with properties {name:"Colname"}
	end tell
	beep 2
	repeat with i from 1 to (count of theSenderList)
		tell theSource
			set theRow to make new data row at the end of the data rows
			set contents of data cell "Colname" of theRow to (item i of theSenderList as text)
		end tell
	end repeat
end addMessage

G’day

I’m getting kind of desperate here folks. I’ve spent 16 hours work trying to get this to work, renaming the table & scroll fields, & window, trying a split view as per the Mail app example, rebuilding the project from scratch etc etc.

Even the following gives the same error.

it always crashes the first time it tries to use the theDataSource variable.

Is there a bug in Apples latest Xcode?



on addMessage(theSenderList)
	tell window "main"
		set theDataSource to data source of table view 1 of scroll view 1 of split view 1
		tell theDataSource
			try
				delete every data column
			end try
			make new data column at end of data columns with properties {name:"Colname"}
			repeat with i from 1 to (count of theSenderList)
				set theRow to make new data row at the end of the data rows
				set contents of data cell "Colname" of theRow to (item i of theSenderList as text)
			end repeat
		end tell
	end tell
end addMessage

I never REALLY learned how to do a table view from a data source, but here’s the workaround handler that I used. This is, of course, from one list, so it’s for the first column in the table.

set theTable to table view 1 of scroll view 1 of window "main"
set content of theTable to {1,2,3,4,5}

There’s some difference here between using “set contents” and “set content”. Yeah, I know. It’s horrible.

G’day, and thanks Experienced.

That works a treat for one column, but the trouble is I’ve got another field with two columns, and two matched lists, so how do i populate the second column please?

Regards

Santa

I’ve tried the following, virtually lifted straight from Apples examples, but all I get is an nscontainerspecifiererror (3)

Does anyone know what’s going on?

I’ve cleaned off every vestige of Xcode and re-installed it, to no avail.

Santa


on will open theObject
	set contactsDataSource to data source of table view "rtfdtable" of scroll view "rtfdscroll" of split view 1 of theObject
	set theWindow to theObject
	tell contactsDataSource
		make new data column at the end of the data columns with properties {name:"name"}
		make new data column at the end of the data columns with properties {name:"address"}
	end tell
end will open

Here we go. To put data in a table with more than one column, you set its contents to a list containing lists of rows. So a list like {{1, 6}, {2, 7}, {3, 8}, {4, 9}, {5, 10}} would be two columns that go {1,2,3,4,5} and {6,7,8,9,10}. Here’s my handler for merging two column lists into that form:

to merge(l1, l2)
	set finalist to {}
	repeat with i from 1 to count l1
		set end of finalist to {item i of l1, item i of l2}
	end repeat
	return finalist
end merge

The fact that a data source is more efficient is all over the docs for this. So I guess that’s what I’m going to learn next.
Then, you’d do this:

set tablConts to merge(col1,col2)
set content of table view 1 of scroll view 1 of window 1 to tablConts

And here’s the follow-up with the data source method! :slight_smile: This works great for me.

on awake from nib theObject
	set theDataSource to make new data source at end of data sources with properties {name:"table"}
	make new data column at end of data columns of theDataSource with properties {name:"kind"}
	make new data column at end of data columns of theDataSource with properties {name:"value"}
	set data source of table view 1 of scroll view 1 of window "main" to theDataSource
end awake from nib

on clicked theObject
	set theDataSource to data source of table view 1 of scroll view 1 of window "main"
	set tablData to merge({1,2,3,4,5},{6,7,8,9,10})
	append theDataSource with tablData
end clicked

to merge(l1, l2)
	set finalist to {}
	repeat with i from 1 to count l1
		set end of finalist to {item i of l1, item i of l2}
	end repeat
	return finalist
end merge

Read this:

http://macscripter.net/articles/451_0_10_29_C/

gives you what you need to get started.

G’day

Thanks fellas, invaluable help. I’ve got my App working, though the 'set to data source ’ still won’t work for whatever reason.

Regards

Santa