A few weeks ago I posted a very similar to the one below. Allen Watson very
kindly sent in a modified version of this script however I am still
receiving errors (Entourage can't continue Myreplace), any further
suggestions would be appreciated.

The script was written by Paul Berkowitz. It solves a problem I have whereby
I run a Lotus Notes script that forwards all mail addressed to me in a Lotus
Notes database on to my Pop mail account. Lotus Notes is not very clever
about mail and does not have the redirect function that Entourage has.

As a result all of the mail forwarded to my POP mail account appears with
the sender as me. The script changes the sender of the note back to the
original sender by parsing the text of the message and extracting the
sender.

Unfortunately our Lotus Notes Administrator has changed the way emails are
presented such that the current script fails.

Specifically if a mail messages contains an attachment (as many do) that
information is presented in the first line of the message.

Secondly the sender's name is presented over a varying number of lines
making parsing difficult. If the sender is from the JPMorgan information
there is no domain appended.

tell application "Microsoft Entourage"
    set theMsg to item 1 of (get current messages)
    if address of sender of theMsg is "[EMAIL PROTECTED]" then
        set theContent to content of theMsg
        repeat with i from 1 to (count paragraphs of theContent)
            if paragraph i of theContent � "" then
                set nameLine to paragraph i of theContent
                repeat while nameLine starts with ">"
                    set nameLine to text 2 thru -1 of nameLine
                end repeat
                try
                    repeat while character 1 of nameLine = " "
                        set nameLine to text 2 thru -1 of nameLine
                    end repeat
                   
                    set AppleScript's text item delimiters to {"<"}
                    try
                        set dName to text item 1 of nameLine
                        try
                            set dName to text 1 thru -2 of dName
                        on error -- no name, but < > around email addrtess
                            set dName to ""
                        end try
                        set eAddress to text item 2 of nameLine -- may error
if no email address
                        set AppleScript's text item delimiters to {">"}
                        set eAddress to text item 1 of eAddress
                    on error
                        if nameLine contains "@" and nameLine does not
contain " " then
                            set dName to ""
                            set eAddress to nameLine
                        else
                            set dName to nameLine
                            set eAddress to address of sender of theMsg
                        end if
                    end try
                    set AppleScript's text item delimiters to {""}
                    set display name of sender of theMsg to dName
                    set address of sender of theMsg to eAddress
                    exit repeat
                end try
            end if
        end repeat
    end if
end tell

The script's function is to take an email originally sent to my lotus notes
account and subsequently forwarded to my pop mail account and put in the
correct sender. In other words we are trying to simulate Lotus Notes
redirecting the mail rather than forwarding it.

The problem arises because our great corporate IT department has changed the
format in which Lotus Notes email is received. I have no power to change the
document design as that is the much cleaner solution. The new Lotus Notes
mail format for a received message is:


>     One attachment (363k)
>                  
>                   CSR News
>                   Releases               To:     News
>                   <NewsReleases@c        Releases 1
>                   sr.com.au>             <NewRelease@csr.
>                                          com.au>
>                   10/09/2001             cc:
>                   10:09 AM               Subject:     CSR
>                                          appoints new
>                                          Chief Financial
>                                          Officer
>                  
> 

> 
> Please find the attached CSR news release titled 'CSR appoints new Chief

It can be seen that if the messages has an attachment that information is
presented in the first line of the message.

A split column format is then used so that the sender information (name and
email address) may be split over a number of lines.

Another example is shown below:

>                   barry_kodesh@
>                   splwg.com            To:
>                                        [EMAIL PROTECTED]
>                   05/09/2001           cc:
>                   03:28 PM             rkellerman@perpetual
>                                        .com.au
>                                        Subject:     Six - A
>                                        - Side & Year End
>                                        Function


Finally a third example from an internal Lotus Notes email is shown as
follows:
>                   Geoff J
>                   Warren               To:     ANGUS R
>                                        WILDBLOOD/OM/ROBERT_FLEMING/GB@F
>                   18/09/2001           lemings Production, Craig R
>                   08:28 AM             Mcguire/JPMCHASE@CHASE, Danny J
>                                        Cremasco/JPMCHASE@CHASE, David

Basically I'm thinking the script needs to be modifed as follows:

1   Check if the first line contains the word attachment. If so ignore it.
2   Otherwise  the next line will either contain part of the name of the
sender or the first part of the email address
3   If the sender has a name the email address will be contained within
angle brackets and can be formed by taking all the of the characters in a
line after an angle bracket up to a space and all the characters on the next
line (ignoring leading spaces) until a  closing angle bracket is
encountered.
4   If the sender has no name but just an email address then the email
address is formed by all the non blank characters on each line until a space
is encountered or until a line's first character is after position 21.
5   If a name is formed but there is no valid email address then the address
is internal and should take the form of
[EMAIL PROTECTED] or in example 3 above
[EMAIL PROTECTED]

In fact the email address or sender name appears to start at position 21 and
continues until a space is encountered.

Sorry for such a long post.

But I use this script every day all day. If my description is unclear please
let me know.

Allen's solution is shown below but is falling over with an error Entourage
can't continue with MyReplace


The following works on the two examples you provided; I just copied them
into two text files, test1.txt and test2.txt, and ran the script against
those files in Finder, rather than as a script in Entourage. I have then
spliced that code into your original script, replacing its guts but keeping
the shell of it. Should work; let me know. It�s possible that certain cases
will fail if they don�t follow the pattern of the two examples very
closely...


tell application "Microsoft EntourageSR1"
    set theMsg to item 1 of (get current messages)
    if address of sender of theMsg is "[EMAIL PROTECTED]" then
        set theContent to content of theMsg
        -- Remove leading blanks
        -- Collapse space runs to single space
        set paras to paragraphs of theContent
        set newstuff to ""
        repeat with aLine in paras
            repeat while aLine begins with " "
                if aLine is not " " then
                    set aLine to text 2 thru -1 of aLine
                else
                    set aLine to ""
                end if
            end repeat
            repeat while aLine contains "  "
                set aLine to myReplace("  ", " ", aLine)
            end repeat
            if length of aLine > 0 then set newstuff to newstuff & aLine &
return
        end repeat
        if ((words 1 thru 3 of newstuff) as text) contains "attachment" then
            set newstuff to myAfter(")", newstuff) -- Discard the attachment
info
        end if
        
        set trialAddress to word 1 of newstuff
        if trialAddress contains "@" then
            set finalAddress to trialAddress -- We are done
            return finalAddress
        else
            set trialAddress to myBefore("To:", newstuff)
            set trialAddress to myReplace(return, "", trialAddress)
            if trialAddress does not contain "@" then
                set part2 to myBetween("<", ">", newstuff)
                set part2 to "<" & myBefore(" ", part2) & myAfter(return,
part2) & ">"
                set trialAddress to trialAddress & " " & part2
            end if
            set finalAddress to trialAddress
        end if
        set nameline to finalAddress
        if nameline does not contain "@" then
            display dialog "Could not parse sender's address"
            return
        else
            if nameline does not contain " " then
                set dName to ""
                set eAddress to nameline
            else
                set dName to myBefore("<", nameline)
                set eAddress to myBetween("<", ">", nameline)
            end if
            set AppleScript's text item delimiters to {""}
            set display name of sender of theMsg to dName
            set address of sender of theMsg to eAddress
            --exit repeat
        end if
    end if
end tell


on myReplace(oldS, newS, theText)
    set od to AppleScript's text item delimiters
    set AppleScript's text item delimiters to oldS
    set temp to text items of theText
    set AppleScript's text item delimiters to newS
    set theText to temp as text
    return theText
end myReplace

to myBetween(firstPart, secondPart, someString)
    set lastpart to myAfter(firstPart, someString) -- What follows firstPart
    set midpart to myBefore(secondPart, lastpart)
    return midpart
end myBetween

to myBefore(subStr, mainStr)
    set od to AppleScript's text item delimiters
    set AppleScript's text item delimiters to subStr
    set temp to text items of mainStr
    set theBefore to item 1 of temp
    set theRest to (the rest of temp) as text
    set AppleScript's text item delimiters to od
    return theBefore
end myBefore

to myAfter(subStr, mainStr)
    set od to AppleScript's text item delimiters
    set AppleScript's text item delimiters to subStr
    set temp to text items of mainStr
    set theRest to item 1 of temp
    set theAfter to (the rest of temp) as text
    set AppleScript's text item delimiters to od
    return theAfter
end myAfter


-- 
David Leitch  JPMorgan [EMAIL PROTECTED]  +612 9220 1609



--
To unsubscribe:               <mailto:[EMAIL PROTECTED]>
To search the archives:
          <http://www.mail-archive.com/entourage-talk%40lists.boingo.com/>

Reply via email to