views:

25

answers:

2

Hi Guys,

I wrote a simple AppleScript which loops indefinitely inside Entourage Inbox and gets subjects of "unread" messages:

tell application "Microsoft Entourage"
activate

repeat with eachMsg in messages of folder named "Inbox"
    if read status of eachMsg is untouched then
        set messageSubject to subject of eachMsg as string

        -- bla bla bla

        -- How to delete the message and proceed with the next one???
    end if

end repeat

Now, the problem is, I want to delete messages after getting the subject. How can I do this? Could you please write me an example?

Thanks again!

A: 

Here is a snippit from an example on Microsoft's Entourage help page (specifically the "Nuke Messages" script):

repeat with theMsg in theMsgs
    delete theMsg -- puts in Deleted Items folder
    delete theMsg -- deletes completely
end repeat 
David Rouse
Thanks for your reply. Yes, I found this example too but if you run this in an infinite loop, AppleScript will through an error after deleting the message because the index of the next message will not be matching. Do you have an idea for this?
TamTam
A: 

Once you delete a message, you have changed the length of the message list, so at some point, you are going to come across an index that no longer exists because you have deleted enough messages. To get around this, you have to (essentially) hard code the loop; get the count of messages, and start from the last message and move up from there. Even though you have deleted a message, the indexes above the current one will always be intact. Untested but is a pattern I've used elsewhere...

tell application "Microsoft Entourage"
activate
set lastMessage to count messages of folder named "Inbox"
repeat with eachMsg from lastMessage to 1 by -1
    set theMsg to message eachMsg of folder named "Inbox"
    if read status of theMsg is untouched then
        set messageSubject to subject of theMsg as string

        -- bla bla bla

        -- How to delete the message and proceed with the next one???
    end if

end repeat

Applescript's "convenience" syntax sometimes isn't, and that's why I usually avoid it altogether.

Philip Regan
It worked! Thanks a lot!
TamTam