Re: Stepping through lines in a variable of text
Re: Stepping through lines in a variable of text
- Subject: Re: Stepping through lines in a variable of text
- From: has <email@hidden>
- Date: Sat, 1 Dec 2001 13:58:22 +0000
Andy Wylie wrote:
>
>
on 1/12/01 12:13 PM, email@hidden at email@hidden wrote:
>
>
> I need to do the same thing, but reading line-by-line through a global
>
> variable of text.
[snip]
>
> That's the way I'd do it in Macromedia Director-Lingo - - - how would I do
>
> this in AppleScript? It doesn't appear to like the "repeat with x = 1"
>
>part.
Just a wee matter of getting the syntax right. (See p249-259 of the
AppleScript Language Guide.)
>
set textvariable to "I am
>
the line to read
>
of text variable"
>
>
set AppleScript's text item delimiters to return
>
set lineStringList to text items of textvariable
>
set AppleScript's text item delimiters to ""
>
>
repeat with i in lineStringList
>
display dialog i
>
end repeat
>
Or:
set textvariable to "I am
the line to read
of text variable"
repeat with i in (every paragraph of textvariable)
display dialog i
end repeat
(See p81 of ASLG for more info on the "paragraph" keyword.)
However...
1. There's a limitation (bug?) in Applescript that causes a stack overflow
error when you try to coerce more than (approx) 4000 text items in a string
into a list.
2. There's another limitation that prevents you using the "word" and
"paragraph" keywords on strings >32KB. You can easily avoid the latter by
using "text items" with return as the delimiter.
It's annoying that these limitations exist at all, but AS - like anything
else - isn't perfect. Hopefully all these problems will one day go away.
(BTW, something that might be of interest: I've written a library -
chunkTextLib - that'll make it easier to work around these problems. It
allows 'smart' breaking of long strings [meaning it won't split in the
middle of a word/paragraph if you don't want it to], allowing you to
process long strings in smaller, safer pieces.)
---
Actually, after a wee bit more thought, I came up with this:
======================================================================
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
repeat with x from 1 to (count theString's text items)
set theLine to theString's text item x
--do something with theLine
end repeat
set AppleScript's text item delimiters to oldTID
======================================================================
It's a bit more verbose than using "every paragraph of", but it won't
suffer any of the limitations mentioned above.
has