Re: styled text to record, ok. But record to styled text?
Re: styled text to record, ok. But record to styled text?
- Subject: Re: styled text to record, ok. But record to styled text?
- From: Nigel Garvey <email@hidden>
- Date: Sun, 25 Jan 2004 01:57:31 +0000
Walter Ian Kaye wrote on Sat, 24 Jan 2004 08:51:34 -0800:
>
At 01:47p +0000 01/24/2004, Nigel Garvey didst inscribe upon an
>
electronic papyrus:
>
> about saving
>
>styled text to file and retrieving it as such.
>
>The list approach is a nice one for this. It's also good for when
>
>you don't know at the time of writing what sort of data you'll be
>
>saving and retrieving
>
>
Hmm... so I can have a bunch of known classes that can be written
>
as-is, and then everything else can be embedded in a list... and then
>
if the data read back starts with "list---+" (where ---+ is 0x00 0x00
>
0x00 0x01 indicating a one-item list) then read it back as list and
>
un-list it. That oughta work, right?
Well I wouldn't like to have to analyse the data formats in the file
itself. :-) My point was that if you know you'll be saving an item of an
unforeknown type, you can save it wrapped in a list and not have to worry
about how to read it back. You read it back 'as list'. After that, it's
relatively easy to test the class(es) of whatever's in the list.
Similarly with multiple items of known type(s). One problem with reading
these back individually from a file is that you have to know how long
each one is. If you've saved two dates, say, the following isn't good
enough:
set date1 to (read fRef as date)
set date2 to (read fRef as date)
The first date's set OK, but the file's read through to the end, causing
an eof error when you try to read the second date. But this does work:
set date1 to (read fRef for 8 as date)
set date2 to (read fRef as date) -- 'for 8' if more data to read
Popping everything into one list saves a lot of hassle and prior research.
Something interesting I've just discovered: the 'write' command's 'as
list' parameter encapsulates rather than coerces:
set r to {a:1, b:2}
write r as list to fRef -- 'write' parameter
read fRef from 1 as list
--> {{a:1, b:2}}
But:
set r to {a:1, b:2}
write (r as list) to fRef -- AS coercion
read fRef from 1 as list
--> {1, 2}
>
I really do wish I could store the datatypes directly, without a list
>
wrapper.
I certainly agree as far as styled text is concerned. :-) Testing in
10.2.8, so far I've had no trouble with text, Unicode Text, lists,
records, dates, aliases, integers, or reals. But only in the case of text
is is the 'as' parameter optional when reading and the type class
specified has to be precisely the same as the class of the saved item.
'As number' doesn't work. So far, I've only found that styled text,
booleans, Wednesdays, and other items whose class is 'class' need to be
"enlisted". I haven't tried application-specific classes yet.
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.