Re: Wrapping lines
Re: Wrapping lines
- Subject: Re: Wrapping lines
- From: has <email@hidden>
- Date: Wed, 5 Dec 2001 11:29:48 +0000
John Fountain wrote:
>
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.
The following handler isn't ready for primetime as I haven't decided how
white space directly before and after wraps should be treated. (Any
suggestions? At the moment it preserves any spaces before the wrap point,
with the option of trimming any additional spaces that lie after the wrap.)
It hasn't been properly tested either.
======================================================================
property breakableChars : {space, tab, "-"}
on wrapText(theString, lineLength, trimWrappedLines) --wraps
[NO-BREAK]theString to lines of max length [lineLength] breaking
[NO-BREAK]after certain permitted characters[breakableChars], with
[NO-BREAK]extra option of trimming white space from start of wrapped
[NO-BREAK]lines
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set theList to {}
theString's text items
repeat with aLine in result
repeat
set theBreak to lineLength
--get first [linelength] characters of [aLine]
try
set theChunk to aLine's text 1 thru theBreak
on error
set theList's end to aLine
exit repeat
end try
--wrap to next line after a breakable character
if theChunk's last character is not in breakableChars then
set minLength to lineLength
repeat with eachChar in breakableChars
set AppleScript's text item delimiters to eachChar as
[NO-BREAK]string
theChunk's last text item's length
if result is less than minLength then set minLength to
[NO-BREAK]result
end repeat
if minLength is less than lineLength then set theBreak to
[NO-BREAK]theBreak - minLength
end if
set theList's end to aLine's text 1 thru theBreak
try
set aLine to aLine's text (theBreak + 1) thru -1
on error
exit repeat
end try
--optionally trim white space at start of wrapped line
if trimWrappedLines then
try
repeat while aLine's first character is space
set aLine to aLine's text 2 thru -1
end repeat
on error
exit repeat
end try
end if
end repeat
end repeat
set AppleScript's text item delimiters to return
set theString to theList as string
set AppleScript's text item delimiters to oldTID
theString
end wrapText
set theString to "The lazy brown fox fell over the quick red dog.
The-lazy-brown-fox-fell-over-the-quick-red-dog.
The_lazy_brown_fox_fell_over_the_quick_red_dog.
The lazy brown fox fell over the quick red dog. "
wrapText(theString, 30, true)
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
Eventually I'll release it onto AppleMods as part of a larger library, but
meantime HTH.
has
p.s. If you're curious, the ScriptToEmail applet also contains a (less
sophisticated) text-wrapping routine.