• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reading and writing records
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading and writing records


  • Subject: Re: Reading and writing records
  • From: Philip Aker <email@hidden>
  • Date: Fri, 24 Aug 2007 21:08:27 -0700

On 2007-08-24, at 15:17:44, Chris wrote:

has wrote:
Chris wrote:

I know how to read and write a list of records to a file using the "as list" qualifier.
[...]
Does anyone have a code snippet for writing a list of records one at a time and reading them back one at a time until eof?

You don't. You slurp the whole list into memory and work on it there. (This is not to say it's impossible, just a very bad idea.)

A more useful question would be: what are you trying to achieve (i.e. what's the script's purpose)? e.g. If you're needing a persistent, filesystem-based data store that allows random access, then using Standard Additions to read and write serialised AppleScript data structures is really the wrong tool for the job and there are much more appropriate solutions that you should look into (plain text files, XMLLib.osax, System Events' Plist Suite, Database Events, FileMaker Pro, PostgreSQL, etc.).

For this particular case, I'm wanting to transfer playcounts in iTunes between different computers. So I don't need random access. The file is short lived. And I don't really see the point of slurping tens of thousands of records into memory when I don't need more than one at once.

The point is that if you want to use AppleScript list and record formatting in this way, you actually have to know the details of the binary file format for both lists and records and then concoct a few handlers to be able to do what AppleScript doesn't handle natively. And it's slow compared to all of the other choices.


However to address what I think is your specific question, there are at least two ways I know how to solve the problem and I'm not certain which would be more appropriate for your case. The criteria are whether or not the records are all the same size and exactly how this "list of records" will be formatted on disk. For the latter, what I mean is either it's:

1. Write an AppleScript list of AppleScript records to disk in such a way that one can append an individual record to the file on disk and read it either as a whole into an AppleScript list or by examining it's records one at a time.

or it's:

2. Write an _array_ of AppleScript records to disk in such a way that one can append an individual record to the file on disk. This format will not handle reading the whole as an AppleScript list. The only choice is to read one record at a time until an eof condition.

So basically, if you present details of your record format, then folks would have a better idea of the best kind of solution.


Can anyone give me a code snippet for testing for eof while reading records? A simple thing that my applescript book doesn't cover.

Unless you have fixed-sized records, Shane's post about using a 'try' handler is really the only choice.


If you can warp the format (on disk) to a fixed-size, then I believe you'll benefit by a speed improvement. See Nigel Garvey's article: <http://macscripter.net/articles/437_0_10_0_C/> for a few ideas.


Philip Aker email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: Reading and writing records
      • From: Chris <email@hidden>
References: 
 >Re: Reading and writing records (From: has <email@hidden>)
 >Re: Reading and writing records (From: Chris <email@hidden>)

  • Prev by Date: Re: Reading and writing records
  • Next by Date: Re: getting duplicates in a list by item id
  • Previous by thread: Re: Reading and writing records
  • Next by thread: Re: Reading and writing records
  • Index(es):
    • Date
    • Thread