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
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.
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
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)
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.
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.
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.
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.
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.
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.
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.
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.