Re: Wrapping lines
Re: Wrapping lines
- Subject: Re: Wrapping lines
- From: Nigel Garvey <email@hidden>
- Date: Wed, 5 Dec 2001 14:10:42 +0000
John Fountain wrote on Tue, 4 Dec 2001 15:23:29 -0500:
>
Greetings all-
>
>
I have a script that I wrote that wraps lines of text if they are more
>
than 31 characters long. It is basically a kludge of if...thens that
>
gets the job done but i can't help but think there must be a cleaner way
>
to do this. Has anyone out there written a script like this before? I am
>
interested to see other attempts at doing this.
Here's a handler I use to quote text when replying to messages in these
digests (as above) or when following up my own e-mail messages. It can
easily be adapted for other line lengths and to omit the quote character.
I've left out most of the comments for legibility, but basically the
method is:
1) Get a list of the paragraphs of the text.
2) For each paragraph, build up a list of the lines into which it will
be broken and then substitute this list for the paragraph in the
paragraph list.
3) Coerce the paragraph list (now a list of lists of lines) to string,
using an appropriate delimiter.
on quoteText(theText)
set lineWidth to 75 -- ie. 75 + the quote character
set theParagraphs to the paragraphs of theText
repeat with thisParagraph in theParagraphs
tell thisParagraph
set paraEnd to count
if the result is greater than lineWidth then
set theLines to {}
set lineStart to 1
set lineEnd to lineStart + lineWidth
repeat while lineEnd comes before paraEnd
repeat with j from lineEnd to lineStart by -1
if character j is " " then exit repeat
end repeat
if j > lineStart then
set the end of theLines to text lineStart thru (j - 1)
set lineStart to j + 1
else
set the end of theLines to text lineStart thru (lineEnd - 1)
set lineStart to lineEnd
end if
set lineEnd to lineStart + lineWidth
end repeat
set the end of theLines to text lineStart thru paraEnd
-- Replace this paragraph in the list with the list of its lines
set contents to theLines
end if
end tell
end repeat
set AppleScript's text item delimiters to {return & ">"}
set quotedText to ">" & theParagraphs
set AppleScript's text item delimiters to {""}
return quotedText
end quoteText
NG