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: Kai <email@hidden>
- Date: 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.
Firstly, if you're trying to find the latest incidence of a character within
a string, it's probably better to work back from the end of the string (as
Ivan suggested), rather than forwards from an arbitrary point somewhere in
the middle of it.
However, it strikes me that comparing each character of a string with a list
of punctuation marks may involve quite a few repeats - especially if a
lengthy passage contains little punctuation. You might instead consider
turning the process on its head - to compare each punctuation character with
the string. That way, each run will always be limited to only 7 repeats (one
for each punctuation character).
This should help to illustrate the kind of approach I mean:
--=========================
to cutLength of s below m
if m is greater than (count s) then return s
set s to s's text 1 thru m
set c to m
set t to text item delimiters
repeat with d in ":,.|?!;"
if d is in s then
set text item delimiters to d
tell s's text items to if (count it) is greater than 1 then
tell (count its item -1) to if it is less than c then set c to it
end if
end repeat
set text item delimiters to t
if c is m then return s
s's text 1 thru -(c + 1)
end cutLength
--=========================
-------------------------------------------------------
(Any wrapped lines abutting the left edge of the window
should be reconnected to the end of the previous line)
-------------------------------------------------------
-------------
Test/results:
-------------
set testString to "This is a test; this is a jest: This is a nest. Is
this a pest? Give it a rest!"
cutLength of testString below 80
--> "This is a test; this is a jest: This is a nest. Is this a pest?
Give it a rest!"
cutLength of testString below 70
--> "This is a test; this is a jest: This is a nest. Is this a pest?"
cutLength of testString below 60
--> "This is a test; this is a jest: This is a nest."
cutLength of testString below 40
--> "This is a test; this is a jest:"
cutLength of testString below 20
--> "This is a test;"
--
Kai
_______________________________________________
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.