Re: Reading Lists and Records from Disk Files
Re: Reading Lists and Records from Disk Files
- Subject: Re: Reading Lists and Records from Disk Files
- From: Richard 23 <email@hidden>
- Date: Sat, 6 Jan 2001 19:57:19 -0800
>
Hi All,
>
>
I am trying to save applescript lists and records to and from disk files
>
without much success. A single list such as {"One","Two",Three"} will come
>
back as expected, but a list of lists such as {{"One", "Two", "Three"},
>
{"Four", "Five", "Six"}} or a record such as {thename:"Popeye",
>
thevice:"Spinach"}
>
will generate an "Out of Memory Error" when reading it back.
>
>
I am aware of the need to coerce the data to the appropriate class when
>
getting it from disk i.e "as list" or "as record", but only text and single
>
lists want to play ball. Any suggestions?
>
>
This is an example which works for single lists but not a list of lists.
>
>
set my_file to open for access file target_file_path
>
set my_data to (read my_file as list)
>
close access my_file
>
>
If you replace "as list" with "as record" (in the case when it is a record
>
you are retrieving), it doesn't work. Interestingly if you remove the
>
coercion, what comes back is the raw data which proves it was a proper
>
record, but then you can't do much with it.
>
>
"reco\\\usrflist\\\TEXT\\\thenameTEXT\\\PopeyeTEXT\\\theviceTEXT\\\S
>
pinw
>
>
ich"
>
>
>
Patrick Mounteney
Is there some advantage to or requirement you store data in a text file?
For the reasons you state and the problems encountered by others (I've
tried many different approaches myself) I tend to limit my use of the
read and write commands to text files.
when I want to store state or settings values I'll build up a little
script object, specifying it's parent as AppleScript and save it using
store script. If there's a particular return format I'm interested in
I'll place a return statement at the end of the script object so that
run script returns the data I'm retrieving without having to query the
object's properties directly. Something like:
-- ----------------------------------------
set filePath to "Private:testData"
StoreData(filePath, {"pretty", {adj:"goofy"}, {1, 2, 3}},
"
http://whocares.com/foo/")
LoadData(filePath)
on StoreData(filePath, searchResults, theUrl)
script
property parent : AppleScript
property File_Path : filePath
property Search_Results : searchResults
return {File_Path, Search_Results}
end script
store script result in file filePath with replacing
end StoreData
on LoadData(filePath)
return run script (filePath as alias)
end LoadData
-- ----------------------------------------
The reason for the parent is to store the script object on its own
without the
extra weight of the containing script which would normally be saved
(although
invisibly) with the script object.
I don't have to worry about tinkering with read and write now or when it
gets
fixed (and the old tricks required now don't work anymore).
If you have nosy neighbors you might want to include a comment in the
script
object mentioning that even though the script appears to be empty it
really
contains data which will be lost if some moron runs, recompiles and saves
the script back to disk.
There is a way to store the script object with the data viewable in the
source
but it would require more explanation than I have the desire to go into
at the
moment and don't really know if you want it anyway.
R23