Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

So I have an AppleScript that iterates over all the messages in various mailboxes and finds messages which meet various criteria and moves them to Deleted Items. The mailboxes in question are associated with an Exchange email account.

the problem is that this script is unbearably slow in ScriptEditor and while it runs it often makes Mail.app unusable. The script runs fine under osascript.

The relevant code fragment is given below

set accountList to {"Gmail"}
set folderList to {¬
{accountName:"Gmail", mailboxName:"INBOX/Stuff", staleDays:21}, ¬
{accountName:"Gmail", mailboxName:"INBOX/Things", staleDays:21} ¬
    }
set emailList to {¬
{accountName:"Gmail", fromAddress:"person@company.com", staleDays:14} ¬
    }
set excludeList to {"Trash", "Send Messages", "Drafts", "Deleted Messages", "Archive", "Deleted Items", "Sent Items"}

set currentDate to current date

tell application "Mail"
repeat with curFolder in folderList
    set curAccountName to the accountName of curFolder
    set mailAccount to account curAccountName
    set mailboxName to mailboxName of curFolder
    set curMailbox to mailbox mailboxName of account curAccountName
    repeat with i from 1 to number of messages in curMailbox
        try
            set curMessage to message i of curMailbox
            set difference to (currentDate - (date sent of curMessage)) div days
            if difference is greater than (staleDays of curFolder) then
                set mailbox of curMessage to mailbox "Trash" of account of mailbox of curMessage
            end if
        end try
    end repeat
end repeat

repeat with curAddress in emailList
    set curAccountName to the accountName of curAddress
    set mailAccount to account curAccountName
    set everyMailbox to every mailbox of mailAccount
    repeat with curMailbox in everyMailbox
        set curMailboxName to the name of curMailbox
        if curMailboxName is not in excludeList then
            repeat with i from 1 to number of messages in curMailbox
                try
                    set curMessage to message i of curMailbox
                    set difference to (currentDate - (date sent of curMessage)) div days
                    if difference is greater than (staleDays of curAddress) then
                        if (sender of curMessage) is (fromAddress of curAddress) then
                            set mailbox of curMessage to mailbox "Trash" of account of mailbox of curMessage
                        end if
                    end if
                end try
            end repeat
        end if
    end repeat
end repeat
end tell

What is slow? Something on the order of 1 message a second or slower. Often much slower. The script finishes in a hour or so (yea, I have a lot of mail) under osascript but I've never gotten the script to complete within 8 hours under ScriptEditor. The weird thing is that at times it may process hundreds of messages a second, then go back to being slow.

Interestingly: sometimes Mail.app is locked up and sometimes it's not. When it's locked up I suspect there's a deadlock issue but it's not consistent. Usually ScriptEditor (I'm running the script from there) and Mail are both using a full core of CPU. ScriptEditor often lists as Unresponsive in Activity Monitor.

There's no evidence that disk performance is an issue, there's plenty of memory free. It really seems to be an odd interaction between ScriptEditor and Mail.

Is there any known reason why this would happen?

Edit: Full code

share|improve this question
    
Hi, I looked over the first script in your question. It wouldn't run at all in AppleScript Editor. I did some "guess work" to get it to run. Will you provide the whole script that both compiles and runs? –  Kaydell Sep 11 '13 at 23:41
    
I edited the full code to change sensitive information. I realize it's not the most efficient organization, but it's hard to refactor something I can't test. –  Michael Conlen Sep 12 '13 at 12:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.