• 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: string Manipulation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string Manipulation


  • Subject: Re: string Manipulation
  • From: Christopher Nebel <email@hidden>
  • Date: Thu, 20 Oct 2005 16:55:44 -0700

On Oct 20, 2005, at 11:25 AM, Walter L. wrote:

Thanks for your response Pete, but I'm still confused about how to read "\n" and "\r" and "return" seperately.

I think Pete's point was that, in general, you shouldn't have to. There are certain exceptions to this, but usually, you've got a bunch of text with line breaks in it, and the exact style of line break is irrelevant. This is precisely what AppleScript's "paragraph" elements do -- \n", "\r", and "\r\n" are all considered equally valid paragraph separators. (Typically, your text will only have one particular style of separator, but these days, who knows for sure.) Now, if you really *do* care, then "text item delimiters" are your friend.


I'd like to be able to control how many returns are put into a new list or string from the original string/list after a line or paragraph. Some emails have multiple returns between paragraphs or returns in mid sentence between punctuation.

I'd like to isolate and break down the text into sections, put each section into a list or new string, then rebuild it controlling the spacing between paragraphs. There would also be no broken sentences.

The breaking-down part is easy: "paragraphs of" will do that for you. Reconstruction is a bit harder, but not too bad. Notice that I said that each occurrence of \n, \r, or \r\n is a separator. This means that two separators in a row will show up as an empty paragraph. For example:


    this is\n
    one sentence\n
    \n
    and here\n
    is another

...would have the pararaph elements "this is", "one sentence", "", "and here", "is another". From this, we can come up with two rules:

1. Two non-empty strings in a row should be joined together with a space between them.
2. One or more empty strings in a row should become a paragraph break.


Here's how that might look in code:

to munge(broken_text)
set new_text to ""
set lastp to ""
repeat with p in paragraphs of email_text
set p to contents of p -- don't ask.
if p is not ""
if lastp is not "" then set new_text to new_text & " "
set new_text to new_text & p
else
if lastp is not "" then set new_text to new_text & return & return
end if
set lastp to p
end
return new_text
end



--Chris Nebel AppleScript and Automator Engineering

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >re: string Manipulation (From: "Walter L." <email@hidden>)

  • Prev by Date: Re: filemaker applescript question
  • Next by Date: Re: Days and hours
  • Previous by thread: Re: string Manipulation
  • Next by thread: re: string Manipulation
  • Index(es):
    • Date
    • Thread