With aid here and reading example code and other references, I’ve learned how to grab text from a file, parse it and put into a table.
Now I have the problem of exporting the data back to a text file after the user has made changes.
I’ve written some code to handle this, but when I run it the text does not output in a format I recognise…anyone know what could be causing this?
–Open this script in a new Script Editor window.
using terms from application “Xcode”
[b][color=blue]on[/color][/b] [color=green]writeData[/color]([color=green]theWindow[/color], [color=green]theObject[/color], [color=green]filePath[/color])
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] [color=green]filePath[/color]
[b][color=blue]set[/color][/b] [color=green]x[/color] [b][color=blue]to[/color][/b] 1
[b][color=blue]try[/color][/b]
--[i][color=olive] Open the file[/color][/i]
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] [color=green]thisFile[/color] [b][color=blue]as[/color][/b] [color=blue]text[/color]
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] [color=blue]open for access[/color] [color=blue]file[/color] [color=green]thisFile[/color] [b][color=blue]with[/color][/b] [color=blue]write permission[/color]
[b][color=blue]try[/color][/b]
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]x[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [color=green]idTotal[/color]
[b][color=blue]set[/color][/b] [color=green]theDataRow[/color] [b][color=blue]to[/color][/b] [color=blue]data column[/color] [color=green]x[/color] [b][color=blue]of[/color][/b] [color=green]theDataSource[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "id" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "headline" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "byline" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "dateCreated" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "imagesToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "graphicsToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "videoToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "audioToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "abstract" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] [color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "catagory" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[b][color=blue]if[/color][/b] [color=green]x[/color] = [color=green]idTotal[/color] [b][color=blue]then[/color][/b]
[color=blue]write[/color] "E|" [color=blue]to[/color] [color=green]thisFile[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b]
[color=blue]display dialog[/color] "Could not write to the file."
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b]
[color=blue]display dialog[/color] "The server didn't respond or does not exist at the specified location."
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[b][color=blue]end[/color][/b] [color=green]writeData[/color]
end using terms from
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
Krioni
July 23, 2004, 2:39pm
#2
What does it look like? I also notice that your are getting the data columns, not the data rows. That could be a problem. I also don’t see in your code where you define the variables idTotal or theDataSource. Are those properties of your script?
Sorry for the confusion. idTotal is a property variable, and at the time of that post, theDataSource was a property of the script as well.
The result text file has each contents of data cell “-” call outputting this
http://s93769967.onlinehome.us/output.jpg
I found the solution, and it was an easy fix (that took me hours to discover, doh!).
All that was required was to to say contents [] as string, so simple, yet so frustrating.
Thank you for your help Krioni, I wouldnt have been able to stumble onto the solution without your advice.
–Open this script in a new Script Editor window.
on writeData (theWindow , theObject , tableView )
try
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] "Data:stories.txt" [b][color=blue]as[/color][/b] [color=blue]alias[/color]
[b][color=blue]set[/color][/b] [color=green]x[/color] [b][color=blue]to[/color][/b] 1
[b][color=blue]set[/color][/b] [color=green]theDataSourceTemp[/color] [b][color=blue]to[/color][/b] [b][color=blue]the[/color][/b] [color=blue]data source[/color] [b][color=blue]of[/color][/b] [color=green]tableView[/color]
[b][color=blue]try[/color][/b]
--[i][color=olive] Open the file[/color][/i]
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] [color=green]thisFile[/color] [b][color=blue]as[/color][/b] [color=blue]text[/color]
[b][color=blue]set[/color][/b] [color=green]thisFile[/color] [b][color=blue]to[/color][/b] [color=blue]open for access[/color] [color=blue]file[/color] [color=green]thisFile[/color] [b][color=blue]with[/color][/b] [color=blue]write permission[/color]
[color=blue]set eof[/color] [b][color=blue]of[/color][/b] [color=green]thisFile[/color] [color=blue]to[/color] 0
[b][color=blue]try[/color][/b]
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]x[/color] [b][color=blue]from[/color][/b] 1 [b][color=blue]to[/color][/b] [color=green]idTotal[/color]
[b][color=blue]set[/color][/b] [color=green]theDataRow[/color] [b][color=blue]to[/color][/b] [color=blue]data row[/color] [color=green]x[/color] [b][color=blue]of[/color][/b] [color=green]theDataSourceTemp[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "id" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "headline" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "byline" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "dateCreated" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "imagesToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "graphicsToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "videoToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "audioToggle" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "abstract" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] ([color=blue]contents[/color] [b][color=blue]of[/color][/b] [color=blue]data cell[/color] "catagory" [b][color=blue]of[/color][/b] [color=green]theDataRow[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color]) [color=blue]to[/color] [color=green]thisFile[/color]
[color=blue]write[/color] "|" [color=blue]to[/color] [color=green]thisFile[/color]
[b][color=blue]if[/color][/b] [color=green]x[/color] = [color=green]idTotal[/color] [b][color=blue]then[/color][/b]
[color=blue]write[/color] "E|" [color=blue]to[/color] [color=green]thisFile[/color]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b]
[color=blue]display dialog[/color] "Could not write to the file."
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b]
[color=blue]display dialog[/color] "The file does not exist at the specified location."
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[color=blue]close access[/color] [color=green]thisFile[/color]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b] [color=green]error_msg[/color] [color=blue]number[/color] [color=green]error_num[/color]
[b][color=blue]try[/color][/b]
[color=blue]close access[/color] [color=green]thisFile[/color]
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[b][color=blue]if[/color][/b] [color=green]error_num[/color] [b][color=blue]is[/color][/b] [b][color=blue]not[/color][/b] -39 [b][color=blue]then[/color][/b] [b][color=blue]return[/color][/b] [color=blue]false[/color]
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
end writeData
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]