Re: Wrapping lines
Re: Wrapping lines
- Subject: Re: Wrapping lines
- From: garbanzito <email@hidden>
- Date: Tue, 4 Dec 2001 17:11:27 -0700
at 2001 12 04, 15:23 -0500, they whom i call 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.
here's mine that i use to wrap all my paragraphs in Eudora.
i'm not holding it up as an elegant example. it assumes
paragraphs are separated by two returns and words by one
space or one return. i've always meant to improve it to
maintain indentation, recognize lists, understand
punctuation better, etc.
property wrap_length : 60 -- my preference for email line-length
on run
tell application "Eudora 5.1 (OS X)"
set new_paragraphs to {}
set orig_text to selected text of window 1 -- front most
-- extract paragraphs & process individually
set {old_delims, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, return & return}
set the_paragraphs to every text item of orig_text
repeat with a_paragraph in the_paragraphs
set new_paragraphs to new_paragraphs & my wrap_text(a_paragraph)
end repeat
set new_text to new_paragraphs as string -- return & return is
still delimiter!
set AppleScript's text item delimiters to old_delims
set selected text of window 1 to new_text
end tell
end run
(*
return the text with all lines wrapped to a maximum of wrap_length
note: uses satimage scripting addition verbs "change" (was
"replace"), "extract string"
*)
on wrap_text(the_text)
-- first unwrap returns and undouble spaces (triple spaces aren't handled)
set my_text to (change " " into " " in (change return into " " in the_text))
set text_length to length of my_text
set this_line to 1
set next_line to wrap_length + 1
set wrapped_text to ""
repeat
-- if we've reached the end, pin the tail on the donkey
if next_line text_length then
extract string my_text from this_line
set wrapped_text to wrapped_text & the result
exit repeat
end if
-- if next line starts with space, this line is broken on exact word boundary
if character next_line of my_text is " " then
extract string my_text from this_line to next_line
set wrapped_text to wrapped_text & the result & return
set this_line to next_line + 1 -- skip leading space
set next_line to this_line + wrap_length
else -- work backwards to end of previous word
set found_space to false
repeat with i from wrap_length to 1 by -1
if (character (this_line + i) of my_text) is " " then
extract string my_text from this_line to this_line + (i - 1)
set wrapped_text to wrapped_text & the result & return
set next_line to this_line + i + 1
set found_space to true
exit repeat -- inner processing of one line
end if
end repeat
-- if no space found, just break at wrap_length
if not found_space then
extract string my_text from this_line to next_line - 1
set wrapped_text to wrapped_text & the result & return
end if
set this_line to next_line
set next_line to this_line + wrap_length
end if
end repeat
return wrapped_text
end wrap_text
--
steve harley email@hidden