TableView - write and read to/from a data file

I have a problem. I want write the contents of every data cell of every data row to a file and then I want to load this file again to my tableview (with 2 data cells). How can I do this?

Many thanks for your help!
vonne

Here is my code. I can write the contents of the table to a file, but when I load the file every cell and every row is the same!

property tableData : {{the_name:"Bart Simpson", the_city:"Springfield", the_zip:"19542", the_age:12}, {the_name:"Ally McBeal", the_city:"Boston", the_zip:"91544", the_age:28}}
property pathName : "TableData"

on awake from nib the_object
	tell the_object to center
	my load_data(tableData)
	show window "main"
end awake from nib

on clicked the_object
	set object_name to name of the_object as string
	if object_name = "write" then
		my writeData()
	else if object_name = "load" then
		my loadData()
	end if
end clicked

on should quit after last window closed the_object
	return true
end should quit after last window closed

on load_data(the_data)
	tell window "main"
		set the_data_source to data source of table view "myTableView" of scroll view "myScrollView"
		tell the_data_source
			try
				delete data rows
			 end try
			 try
				delete data columns
			 end try
			 make new data column at end of data columns with properties {name:"the_name"}
			 make new data column at end of data columns with properties {name:"the_city"}
			 make new data column at end of data columns with properties {name:"the_zip"}
			 make new data column at end of data columns with properties {name:"the_age"}
		end tell
		append the_data_source with the_data
		tell table view "myTableView" of scroll view "myScrollView" to update
	end tell
end load_data

on writeData()
	tell window "main"
		set the_data_source to data source of table view "myTableView" of scroll view "myScrollView"
		tell the_data_source
			set informationFromEveryCell to contents of every data cell of every data row of the_data_source
			set theFile to open for access (pathName as POSIX file) with write permission
			set toSave to {tabellData:informationFromEveryCell}
			write toSave to theFile
			close access theFile
		end tell
	end tell
end writeData

on loadData()
	set theFile to open for access (pathName as POSIX file)
	set theData to read theFile
	close access theFile
	tell window "main"
		set the_data_source to data source of table view "myTableView" of scroll view "myScrollView"
		tell the_data_source
			set contents of every data cell of every data row of the_data_source to theData
		end tell
	end tell
end loadData

does really nobody can help me? :frowning:

Please, i need your help… many thanks!

Vonne,

I don’t understand what you’re trying to do. Perhaps that is why no one has answered. Your script seems to indicate you have 4 columns, but you mention “two data cells.”

Can you describe more fully what you are trying to do, and then what is happening with the code you have now?

I have a app with a tableview with 4 columns! Sorry… :slight_smile:

I want to save all contents of the tableview to a file and I would like to be able to load the file to my tableview again.
But now, when i load the .txt-file with the contents from tableview to my tableview, there are in every data cell the complete contents from the file…
When i load the file to my tableview i want to have the contents from the column 1 in column 1 an from column 2 in column 2 and so on…

I hope you understand now, what my problem is…

Sorry for my bad english! :slight_smile:

Hi,

do you dont understand me not yet?
or can really nobody help me?

I need your help!

Many thanks…

Hi,

now i can save the contents from my tableview to a file like this:


the_name:Bart Simpson the_city:Springfield
the_name:Bart Simpson the_city:Springfield
the_name:Bart Simpson the_city:Springfield

but how can i load this file again to my tableview with two columns (the_name & the_city)

Now can anybody help me? I hope so… :slight_smile:

Many thanks for your help!

That´s my script:


property tableData : {{the_name:"Bart Simpson", the_city:"Springfield"}, {the_name:"Bart Simpson", the_city:"Springfield"}, {the_name:"Bart Simpson", the_city:"Springfield"}}
property tableData2 : {{the_name:"Judy Sorn", the_city:"NYC"}}
property the_arrow : missing value
property pathName : "TableData"

on awake from nib the_object
	tell the_object to center
	my load_data(tableData)
	show window "main"
end awake from nib

on clicked the_object
	set object_name to name of the_object as string
	if object_name = "quit" then
		quit
	else if object_name = "data1" then
		my load_data(tableData)
	else if object_name = "load" then
		my get_prefs()
	else if object_name = "data2" then
		my load_data(tableData2)
	else if object_name = "save" then
		my save_data()
	end if
end clicked

on should quit after last window closed the_object
	return true
end should quit after last window closed

on load_data(the_data)
	tell window "main"
		set the_data_source to data source of table view "myTableView" of scroll view "myScrollView"
		tell the_data_source
			try
				delete data rows
			end try
			try
				delete data columns
			end try
			make new data column at end of data columns with properties {name:"the_name"}
			make new data column at end of data columns with properties {name:"the_city"}
		end tell
		append the_data_source with the_data
		tell table view "myTableView" of scroll view "myScrollView" to update
	end tell
end load_data

on save_data()
	set p to open for access (pathName as POSIX file) with write permission
	set the_data_source to data source of table view "myTableView" of scroll view "myScrollView" of window "main"
	set eof p to 0
	tell the_data_source
		set every_data_row to every data row of the_data_source
		repeat with i in every_data_row
			set the_name to contents of data cell 1 of i
			set the_city to contents of data cell 2 of i
			my write_line(("the_name:") & (the_name) & " " & ("the_city:") & (the_city), p)
		end repeat
	end tell
	
	--set informationFromEveryCell to contents of every data cell of every data row of the_data_source
	close access p
end save_data

on write_line(a, b)
	write a to b
	write (ASCII character 10) to b
end write_line