HELP: need to save table view contents as a text file.

Could anyone help?

:frowning:

I need to save the contents of a table view as a text file, which i can load into the table view again later.
Thanks in advance to any help.

You can simply write the contents (which is a list) of the data source of the table to a file and read it back again, like this:

set myDataList to contents of data source of table view "My Table"
set dataFile to POSIX file "/Users/Shared/saved myDataList"
set writeRef to open for access dataFile with write permission
try
	set eof writeRef to 0
	write myDataList to writeRef
	close access writeRef
on error errorMessage
	close access writeRef
	error errorMessage
end try

You can later read the data back from the file to a list and, if needed, dump the list back into the data source of the table by:

set myDataList to read dataFile as list
set contents of data source of table view "My Table" to myDataList

Tom
BareFeet
http://www.tandb.com.au

thanks, but currently, im not using any data source. is there anyway to do this on a table without a data source? thanks

I’m fairly sure that even though you may not have explicitly created a data source, Xcode creates it for you, so the code I posted should work. Try it and see.

Tom
BareFeet
http://www.tandb.com.au/applescript/

i tried the code above, i think i’m getting an error in the following line:

set myDataList to contents of data source of table view “My Table”

is there a need for me to specify 
 of data source of table view “my table” of scroll view “my table” of window “my window”?

i’m new to applescript and i dont really know much about naming.

Yes. I didn’t specify the hierarchy since I had no way of knowing where your table view is positioned, sorry. You need to tell it where the table view is, since there could be many in your project, or many instances (eg one in each window). You can only ultimately refer to objects that are elements in the application. The application contains windows, which can contain views such as a scroll view which can contain a table view. And a table view has a data source property. These relationships are depicted in the dictionary (“AppleScriptKit.sdef” in the Xcode window).

I’m surprised you posted this question since just testing the alternative you mentioned should work in your situation :wink:

Tom
BareFeet
http://www.tandb.com.au/applescript/

:stuck_out_tongue: i tried to use it before posting, but it seems like it doesn’t work. could it be because there really is no data source?
the error im getting is: Can’t get «class scrV» “TCTable”. (-1728)

thanks for the reply.

Have you named your scroll view “TCTable” in Interface builder? You have to for that to work. Note that the table view and scroll view have to have the names in Interface builder that you specify in your code.

Please post your actual full code that deals with this.

Tom
BareFeet
http://www.tandb.com.au/applescript/


on clicked theObject
	if name of theObject is "b1" then
		set myDataList to contents of table view "TCTable" of scroll view "TCTable"
		set dataFile to POSIX file "Data:tbview.sav"
		set writeRef to open for access dataFile with write permission
		try
			set eof writeRef to 0
			write myDataList to writeRef
			close access writeRef
		on error errorMessage
			close access writeRef
			error errorMessage
		end try
	end if
end clicked

the code above is more or less the code i’m using.
the interface only has 1 table view and a button used to save the contents of the table view to text.
thanks

You didn’t answer my question above. Have a really good look, because the error you’re getting basically means that the system can’t find any scroll view named “TCTable” in the location tat you’ve specified. Note that the table view and scroll view are named independently in Interface builder. You can name them the same thing, but to do that you have to explicitly fill in both fields the same.

What happened to the:

set myDataList to contents of data source of table view "My Table" of scroll view "my table" of window "my window"

syntax that we discussed before? You haven’t specified the window in which the scroll view resides. So that would be the reason (or one of them) why it says it can’t find it.

If your button is in the same window as your scroll view, then what you need is something like this:


on clicked theObject
	set theWindow to window of theObject
	if name of theObject is "b1" then
		set myDataList to contents of data source of table view "TCTable" in scroll view "TCTable" in theWindow
		-- etc

But, make sure that you named the scroll view (as well as the table view) “TCTable” in Interface Builder.

Tom
BareFeet
http://www.tandb.com.au/applescript/


on clicked theObject
	set theWindow to window of theObject
	if name of theObject is "b1" then
		set myDataList to contents of data source of table view "TCTable" in scroll view "TCTable" in theWindow
		set dataFile to POSIX file "Data:tbview.sav"
		set writeRef to open for access dataFile with write permission
		try
			set eof writeRef to 0
			write myDataList to writeRef
			close access writeRef
		on error errorMessage
			close access writeRef
			error errorMessage
		end try
	end if
end clicked

the code above is the entire code of the script i’m working on. now the error i’m getting is:

Can’t get «class datS» of «class tabW» “TCTable” of «class scrV» “TCTable” of window id 1. (-1728)

im sure i named them right scroll view is “tctable” and table view is “tctable”, i dont really understand this error message. does it mean that it cannot find the data source?
BTW, is there a difference using “TCTable” of scroll view, and “TCTable” in scroll view?

thanks for keeping up with me on this old topic. i really appreciate it.

I don’t understand. Your table has data in it, so obviously you have that data in a variable because you had to write it to the table
 and if so then why are you trying to read the data from the table? Why not just use the data that you already have in the variable?

Your variable must be a list of some kind, so just convert that list to a string and write it. Is your variable something like {{1,2}, {3,4}, {5,6}}? That can be converted easily to a string for writing to a text file. Just use a repeat loop and applescript’s text item delimiters.

You know you don’t even have to write it to a file as a string, you can write it in any format
 like a list or a record.

yep but i’m adding data to the table view one at a time, it is very much similar to the Table example included in the xcode installation where you fill out a from and add the data from the form to the table view with a click of a button. i want to save the data that are added to the table, then read the saved file and load it back to the table when necessary.

Thanks.

That makes sense, but you can always write the data to a variable before you post it to the table. Or you can just read the data manually. Get the number of rows of the table, then use a repeat loop to read each row one at a time
 adding it to a variable for writing to file.

I never tried this, but have you tried


set myVariable to content of myTable?

yep i also tried that, error -4960 comes out. i can’t seem to find the description of this error. do you have any idea about it? i’m pretty much stuck on this problem (saving contents of table to file) since i can’t find any resources on the internet on this issue.
:smiley:

By the way, here’s 2 handlers for you that will allow you to read/write to file as something other than as a string
 using the ‘mode’ variable. Just set the mode as list or record, depending on how you want to do it.

on writeTo(this_data, target_file, append_data, mode) -- append_data is true or false, mode is string etc. (no quotes around either)
	try
		set target_file to target_file as Unicode text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof as mode
		close access the open_target_file
		return true
	on error errorText number errorNumber
		log " Error in writeTo: " & errorNumber & ": " & errorText
		try
			close access file target_file
		end try
		return false
	end try
end writeTo
on readFrom(target_file, mode)
	try
		target_file as alias -- if file doesn't exist then you get an error
		set open_target_file to (open for access file target_file)
		set the_data to read open_target_file as mode
		close access open_target_file
		return the_data
	on error errorText number errorNumber
		log "Error in readFrom: " & errorNumber & ": " & errorText
		try
			close access open_target_file
		end try
		return false
	end try
end readFrom

I’m going to give you a method of getting the reference for your table view so that you’re sure you have it right.

#1. in interface builder hook the table view (not the scroll view) up to the awake from nib handler
#2. in awake from nib use this:
if name of theobject is “my_table_name” then
set myTable to theobject
end

#3. make mytable a global variable so you can access it from anywhere such as making it a property like so

property myTable : null

Now whenever you want to do something to the table use myTable
 you don’t have to use anything else
 no tableview “whatever” of scroll view “whatever”
 just use the variable because when you set it to the object it automatically gets the reference to the table.

Then to get the data from your table:
set rowCount to number of rows of myTable
set myVariable to {}
repeat with i from 1 to rowCount
tell data row i of myTable
set thisRow to {content of data cell 1, content of data cell 2} → etc. up to the number of columns
set end of myVariable to thisRow
end
end

Then you have your variable
 and you can use the handers I posted above to write it

writeTo(myVariable, the:file:path, false, list)

Note: I haven’t tried this so you may need to tweak it but it should at least get you close.

thanks for all your replies, though i havent solved the problem yet. I’ll skip this and move to the next one.

:stuck_out_tongue:

I don’t know why you’re coming unstuck. I just now built a new project in Xcode to try to match yours. I created a default Applescript application. In the default window I created a table view, named the scroll view and the table view “TCTable”. Then, for testing, I created three buttons: Fill, Write and Read, named as such as with the clicked handlers linked to the code. Then I entered this code:

property dataFile : POSIX file "/Users/Shared/tbview.sav"

on clicked theObject
	set myName to name of theObject
	set myWindow to window of theObject
	tell myWindow
		tell table view "TCTable" in scroll view "TCTable"
			if myName is "Fill" then
				set contents to {{1, 2}, {3, 4}}
			else if myName is "Read" then
				set myDataList to read dataFile as list
				set contents to myDataList
			else if myName is "Write" then
				set myDataList to contents
				set writeRef to open for access dataFile with write permission
				try
					set eof writeRef to 0
					write myDataList to writeRef
					close access writeRef
				on error errorMessage
					close access writeRef
					error errorMessage
				end try
			else
				error "Unknown button: " & myName
			end if
		end tell
	end tell
end clicked

I ran it, clicked Fill, then Write, changed some of the cells, clicked Read, and it read the data off the disk, overriding my changes correctly.

Tom
BareFeet
http://www.tandb.com.au/applescript/

Hi,

Just read your post. I think its getting close, but there’s a small error. what would cause it to have a “AppleEvent handler failed” error? pardon me coz im really new to applescript (and to mac) . thanks.