Re: Read/Write 'as short' and 'write "" starting at'
Re: Read/Write 'as short' and 'write "" starting at'
- Subject: Re: Read/Write 'as short' and 'write "" starting at'
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 31 Jul 2001 09:49:32 -0700
On 7/31/01 8:37 AM, "Arthur J Knapp" <email@hidden> wrote:
>
Most frustrating of all, of course, is the fact that none of this
>
is documented anywhere. Bill Cheeseman's report on the newest OSs
>
does mention changes with regard to "using delimiters" and the
>
reading/writing of lists and records, but someone, somewhere, really
>
needs to sit down with the read/wrtie commands and document their
>
current functionality, especially with regard to the "as" parameter.
>
I think that both of the Chrises did give us detailed run-downs on this
mailing list of the read/write functionality, including either a couple of
bugs that remain to be fixed (concerning, I believe, the 'as' parameter),
when AS 1.6 was released. I imagine that there will be a major documentation
upgrade sometime after the OS X scripting is itself upgraded in OS 10.1.
Here's hoping, anyway.
Here's a message from Chris Nebel on May 7, 2001, that I kept:
>
Yow! A lot of different problems with read/write being discussed here -- let
>
me attempt to tackle all of them. Read/write was totally rewritten in
>
AppleScript 1.6 and works a lot better, though there are a couple of known
>
bugs.
>
>
First, what's the same:
>
>
- There is no difference between "using delimiter" and "using delimiters" --
>
the compiler considers them to be the same thing and will always remove the
>
trailing "s".
>
>
- delimiters and the parameters for "before" and "until" are assumed to be
>
one-character strings. (One-byte, really -- sorry, Asian users!) If you use
>
a longer string, the later characters will simply be ignored.
>
>
- You may specify at most one of "to", "for", "before", or "until".
>
>
- Using "before", "until", or "using delimiters" will treat the file as text
>
-- if you specify an "as" type, it will take the appropriate chunk of text
>
and try to coerce it to the desired type. For example:
>
>
-- f contains "19 23 42"
>
read file "f" as integer using delimiter " " --> {19, 23, 42}
>
>
Otherwise, the file is treated as binary data.
>
>
>
What's different:
>
>
- Pre-1.6, delimiter lists could be at most 2 items long; in 1.6, they can be
>
any length. The dictionary still says 2 items max, but it lies.
>
>
- Pre-1.6 requires that if you use "using delimiters", you must also have an
>
"as" parameter. 1.6 removes this requirement; if you omit the "as", it
>
assumes "as text".
>
>
- If you use "as list" pre-1.6, you may not use *any* of the positioning
>
parameters: "from", "to", etc. The command always reads from the current
>
position to the end of the file. 1.6 removes this restriction, though you'll
>
probably want to avoid "as list" for text files because of a bug (see
>
below). If you're reading a binary list (i.e. "read as list" without using
>
delimiters), you can't use "to" or "for", because the list data itself tells
>
you how much to read.
>
>
- Pre-1.6, you couldn't write a record or a list that contained other lists
>
"as record" or "as list" (i.e., as binary data) and then read it back
>
successfully. A related bug wouldn't let you read more than one binary list
>
or record from the same file -- the first one would read all the remaining
>
data. Both of these work in 1.6.
>
>
>
What's broken:
>
>
- "as list" combined with "using delimiter" does the wrong thing -- it
>
returns a list of one-item lists, each of which contains the string you
>
should have gotten. Using "as text" instead will return the right thing with
>
either version. For example:
>
>
-- f contains "This is a test"
>
read file "f" as list using delimiter " "
>
--> {"This", "is", "a", "test"} on 1.4; {{"This"}, {"is"}, {"a"},
>
{"test"}} on 1.6
>
read file "f" as text using delimiter " "
>
--> {"This", "is", "a", "test"}
>
>
- Using "as <type>" with "before" or "until" and *no* delimiter will return
>
garbage or a "can't coerce" error. To work around this, read without the
>
"as" and then do the coercion in AppleScript. For example:
>
>
-- f contains "23 42"
>
read file "f" before " " as integer --> Can't make some data into the
>
expected type. (wrong!)
>
read file "f" before " "
>
the result as integer --> 23 (right!)
>
>
I know what the problem is for both bugs, and they're simple, so they'll get
>
fixed in the next revision. Also, I won't be gutting and rewriting
>
read/write again, so there won't be good opportunities to introduce more
>
bugs. ;)
>
>
>
--Chris Nebel
>
AppleScript Engineering
--
Paul Berkowitz