• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Neatly break a paragraph into smaller strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Neatly break a paragraph into smaller strings


  • Subject: Re: Neatly break a paragraph into smaller strings
  • From: Joseph Weaks <email@hidden>
  • Date: Sat, 28 Jun 2003 01:36:08 -0500

On Friday, June 27, 2003, at 03:16 PM, Nigel Garvey wrote:
Oops! Can't fool around with m and get away with it. Here's a repaired version:

Thanks to y'all for the continued enhancement. I was wondering, Nigel, why the first script used the same variables for different uses. When I looked at your penultimate version, I was thinking it would make each sub-paragraph's maximum length shorter and shorter each iteration.
Here's my final version. I separated out the two functions that y was serving, and as always I made variable names more descriptive. All of this is to compensate for my smallmindedness. (ie. x and y are always the text positions in the main string "eachParagraph".) I reserved z for the backwards count you came up with. Doing so fixed an error that occurred when the second or more pass was a subparagraph string that had NO punctuation.
Also, I am doing the processing of the smaller chunks each pass within the handler itself, cause I quickly received stack overflow errors when the handler returns a list.
Thanks again,
Joe Weaks

on splitAndProcess(eachParagraph, maxParagraphLength)
set tid to text item delimiters
set x to 1
set y to maxParagraphLength
set paragraphLength to (count eachParagraph)

repeat until x > paragraphLength
if y > paragraphLength then
set y to paragraphLength
else
set subParagraph to eachParagraph's text x thru y
set z to (count subParagraph)
repeat with d in {": ", ", ", ". ", "| ", "? ", "! ", "; "}
set text item delimiters to d's contents
tell (count subParagraph's text item -1)
if it is less than z then set z to it
end tell
end repeat
-- If no punctuation found in second half of this subParagraph,
-- then break at last 'space'
if z > (maxParagraphLength div 2) then
set text item delimiters to space
set z to (count subParagraph's text item -1)
end if
set y to y - z
end if

set thisChunk to text x thru y of eachParagraph

-- Do stuff with this chunk.

set x to y + 1
set y to x + maxParagraphLength
end repeat
set text item delimiters to tid
end splitAndProcess
_______________________________________________
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.

References: 
 >Re: Neatly break a paragraph into smaller strings (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Newbie blues (was Remove spaces)
  • Next by Date: Re: Newbie blues (was Remove spaces)
  • Previous by thread: Re: Neatly break a paragraph into smaller strings
  • Next by thread: Tab delimited data file to FMP
  • Index(es):
    • Date
    • Thread