Using applescript to clean up e-mails

 
This little tutorial will lead you though the steps of making a script to
take out those nasty >>>>  and short lines in forwarded e-mail.

This will let you make changes to the text easier, and print much nicer!

Download scripts and files

If you would like to just use the scripts without typing them in

Cleaners download


This contains 3 files
  
    Clean Simple-        Takes the clipboard contents and removes line breaks
    Clean Complete-    Same as above, but gets rid of ">", then opens appleworks
                                   and pastes in a new document.  
    Sample Text-          A document that is a prime example of what this program
                                    is good for



The cuplrit, forwarded e-mail

Who has not gotten a e-mail that looks like this?

*****
From: John Doh
To: Macintosh user
Sent: by pony express
Subject: Groovy

Thought you might like this...

> >This is a good  example of an e-mail
> >document
> >that needs a lot of help because
> >too
> >many people thought this was a really
> >neat
> >Joke.  That’s great except now it is really
> >really
> >hard to read, not to mention would take
> >up
> >most of the page of your printer if you tried
> >to
> >print it out on most printers!

-John Doh
*****

With applescript you can painlessly turn that garbage into
nice looking garbage!


Starting with a simple script

We'll start with an easy script.


You can either open the script editor and copy the following in there, or
open "Clean Simple" with the script editor.


--------------------start copy on next line
on run
    set x to (ASCII character 13) --carrage return
    set y to (ASCII character 10) --line feed
   
    set the message_text to the clipboard --copy text from the clipboard
    set the message_text to ¬
        replace_chars(message_text, (x & x), "zebra123") --save double returns
    set the message_text to ¬
        replace_chars(message_text, (x), " ") --turn single returns to a space
    set the message_text to ¬
        replace_chars(message_text, "   ", " ") --take triple spaces out
    set the message_text to ¬
        replace_chars(message_text, "  ", " ") --take double spaces out
    set the message_text to ¬
        replace_chars(message_text, "zebra123", (x & x))
   
    set the clipboard to message_text --put text back on clipboard
end run

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars
-------------------end copy


what this script does is:

    1.  Saves the double line breaks as "zebra123"

    2.  Replaces the remaining line breaks with a space

    3.  Puts the double line breaks back


the comments have a "--" before them

To run the script, copy something to the clipboard, double click on the script
and paste into an application.

Summary of simple script

If you run this on the sample text, you get

*****
From: John Doh To: Macintosh user Sent: by pony express Subject: Groovy

Thought you might like this...

> >This is a good example of an e-mail > >document > >that needs a lot of help because > >too > >many people thought this was a really > >neat > >Joke. That’s great except now it is really > >really > >hard to read, not to mention would take > >up > >most of the page of your printer if you tried > >to > >print it out on most printers!

-John Doh
*****

It got rid of the returns, but it does not look very nice


Round 2 :  Complete Clean

The above script gets the job done, but the result is not always pretty.

The next script does a few things to help make e-mails look a lot better


Once again, You can either open the script editor and copy the following in there, or
open "Clean complete" with the script editor.


--------------------Start copy on next line
on run
    set x to (ASCII character 13)
    set y to (ASCII character 10)
   
    set the message_text to the clipboard
   
    set the message_text to ¬
        replace_chars(message_text, (x & y), (x)) --change from dos formating to mac
   
    set i to 1 --{loop 10 times}
    repeat until i > 10
        set the message_text to ¬
            replace_chars(message_text, (x & " "), (x)) --get rid of leading spaces
        set the message_text to ¬
            replace_chars(message_text, (x & ">"), (x)) --get rid of those silly  >
        set the message_text to ¬
            replace_chars(message_text, (x & "  "), " ") --get rid of double spaces
        set i to i + 1
    end repeat
   
   
   
    set the message_text to ¬
        replace_chars(message_text, (x & x), "zebra123") --save the double returns, those split the paragraphs
   
   
    set the message_text to ¬
        replace_chars(message_text, (x), " ") --this is the meat of it, take out those returns! (but put a space in)
    set the message_text to ¬
        replace_chars(message_text, "     ", " ") --take out 5 spaces
    set the message_text to ¬
        replace_chars(message_text, "   ", " ") --take out 3 spaces
    set the message_text to ¬
        replace_chars(message_text, "  ", " ") --take out 2 spaces
   
   
   
    set the message_text to ¬
        replace_chars(message_text, "zebra123", (x & x)) --put that paragraphs back in

    set the message_text to ¬
        replace_chars(message_text, "From:", (x & "  From:")) --Usually starts a paragraph
    set the message_text to ¬
        replace_chars(message_text, "Sent:", (x & "  Sent:")) --Usually starts a paragraph
    set the message_text to ¬
        replace_chars(message_text, "To:", (x & "  To:")) --Usually starts a paragraph
    set the message_text to ¬
        replace_chars(message_text, "Subject:", (x & "  Subject:")) --Usually starts a paragraph
    set the message_text to ¬
        replace_chars(message_text, "Sender:", (x & "  Sender:")) --Usually starts a paragraph
   
   
    set the clipboard to message_text

    tell application "AppleWorks 6"
        activate
        set doc1 to make new document
        paste
       
        --The next two lines set the font size and font
        --set size of doc1 to 18
        --set font of doc1 to "times"
       
       
        --the next line would print the document
        --print front document
       
        --the next line would close the document you just made
        --close doc1 saving no
       
       
    end tell
   
   
   
end run

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars
----------------------end copy


Wow, that's a long script!

What was added?

  1.  Taking out the > and spaces, this uses a loop to do it 10 times
       So if you have more than 10 >s please increase that number to
       go though the loop more  times!

  2.  If there is "from" or "subject" put them on their own line

  3.  Open up Appleworks, and paste into a new document


Note, I commented out some goodies that would change the font and
font size, print the document, and close the document you just made.


Pretty nice, just put this script in your dock. Then, to fix an e-mail,
copy the e-mail to the clipboard, and click on the script in the dock.  

How am I looking now??


If you run it on the sample text, Appleworks pops up with this:

*****
  From: John Doh
  To: Macintosh user
  Sent: by pony express
  Subject: Groovy

Thought you might like this...

This is a good example of an e-mail document that needs a lot of help because too many people thought this was a really neat Joke. That’s great except now it is really really hard to read, not to mention would take up most of the page of your printer if you tried to print it out on most printers!

-John Doh
****


Much better, and all it takes is a copy and a click!  Note that the text is still on the clipboard after it puts it in Appleworks.