Save contents of Table View to file

I am making a cocoa applescript osx app in Xcode 5

I have a Table View that is updated with the current play time of a quicktime file with each button press using an Array Controller. All is working well but I am stuck on how to then save the results of the Table view to file, preferably a .csv

I have been trying to research ways to do this but most of the tutorials I find relate to iOS. I have researched using NSStrings and writeToFile but am having trouble filling in the blanks.

I have tried binding the table content of the table view to an Array Controller in IB with a Key Path of FinalResults and then on the save button sender

FinalResult's writeToFile:FilePath

FilePath being a location set using

set FilePath to choose folder

It is building successfully but when I press the Save button it is giving me the following error

014-03-11 22:37:07.462 SMPTE Grabber[4043:303] * -[AppDelegate BackUp:]: Unrecognized function writeToFile_. (error -10000)

Do I need to somehow bind the table content to something else??

I have been at this for days now…

If anyone could give me some pointers or know of a tutorial that relates to this I would greatly appreciate it.

Thanks

B

Hi.

the contents of an array controller (aka arrangedObjects()) is usually a list/array of records/dictionaries.
The method to write an instance of NSArray to disk is writeToFile:atomically:, the format of the resulting file is property list

Now I am getting the error

2014-03-12 13:28:03.631 SMPTE Grabber[1325:303] *** -[AppDelegate BackUp:]: The variable atomically is not defined. (error -2753)

??

B

the syntax should look like

arrayController's arrangedObjects()'s writeToFile:"/path/to/file.plist" atomically:true

arrayController is the name of the property connected to the NSArrayController instance

Stefan

Thankyou very much, I have been trying so many different approaches and getting nowhere!

is it possible to convert the plist in to a .csv

Yes, but you have to do it programmatically, because the array controller structure as well as the CSV format can vary

Thanks Ill do some research

B

I worked out how to do this all on my own…

Only joking, StefanK answered it in another post :wink:

I thought would share the results here in case anybody was searching for the same thing

set filePath to POSIX path of (path to desktop) & "test.csv" -- sample path, change it something appropriate
set theArray to arrayController's arrangedObjects() -- arrayController is a property connected to an NSArrayController instance
set csvList to current application's NSMutableArray's array()
csvList's addObject:((theArray's objectAtIndex:0)'s allKeys()'s componentsJoinedByString:",")
repeat with anItem in theArray
   (csvList's addObject:((anItem's allValues())'s componentsJoinedByString:","))
end repeat
set csvText to (csvList's componentsJoinedByString:return)
csvText's writeToFile:filePath atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

Thanks again Stefan

B