Re: Neatly break a paragraph into smaller strings
Re: Neatly break a paragraph into smaller strings
- Subject: Re: Neatly break a paragraph into smaller strings
- From: Nigel Garvey <email@hidden>
- Date: Fri, 27 Jun 2003 20:08:37 +0100
Kai wrote on Fri, 27 Jun 2003 04:12:21 +0100:
>
on Wed, 25 Jun 2003 23:56:18 -0500, Joseph Weaks <email@hidden> wrote:
>
>
> I'd like to improve a portion of script that receives a paragraph and
>
> breaks it into strings under a certain size for further processing.
>
> Virtually all paragraphs will be less than 1000 characters. I had a decent
>
> script snippet until I added lines to make the breaks only at certain
>
> punctuation marks. The script still works, but it sure looks ugly. (I'm
>
> sure this is the perfect example of a mini-applescripter doing things the
>
> hard/slow way.) Suggestions on a better way to do this?
>
>
A couple of thoughts on the general method, Joe.
Here's a development of Kai's excellent offering. It's recursive and
returns a list of all the shorter strings into which the paragraph has
been divided (assuming that the division into paragraphs has already been
done). Further refinements: 1) it only breaks at punctuation characters
that are followed by spaces; 2) it only breaks at these if they occur in
the second half of the segment being examined.
>
-------------------------------------------------------
>
(Any wrapped lines abutting the left edge of the window
>
should be reconnected to the end of the previous line)
>
-------------------------------------------------------
to chopText of s below m
if m is greater than or equal to (count s) then return {s}
set subS to s's text 1 thru m
set c to m
set t to text item delimiters
-- Search for these particular punctuations followed by spaces
repeat with d in {": ", ", ", ". ", "| ", "? ", "! ", "; "}
set text item delimiters to d's contents
tell (count subS's text item -1)
if it is less than c then set c to it
end tell
end repeat
-- If no such punctuation in the second half of this segment,
-- find the last space instead
if c > (m div 2) then
set text item delimiters to space
set c to (count subS's text item -1)
end if
set text item delimiters to t
-- Truncate this segment if a suitable cut's found
if c < m then
set m to m - c
set subS to subS's text 1 thru m
end if
{subS} & (chopText of (s's text (m + 1) thru -1) below m)
end chopText
set testString to "This is a test; this is a jest: This is a nest. Is
this a pest? Give it a rest!"
chopText of testString below 70
--> {"This is a test; this is a jest: This is a nest. ", "Is this a
pest? Give it a rest!"}
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.