Exponential Slowdown of Applescript

applescriptmail.appscript

So I've been working on a simple AppleScript to try to help with managing my e-mail in OS X's Mail, and I have it seemingly working just fine with one major problem; the performance of the script is horrible.

Now, I know AppleScript isn't always the fastest scripting language, but I'm just not sure why the performance is as bad as it is. Even stranger, the length of time to complete each operation in a loop appears to be increasing over time! For example, my script should match about 300 e-mails (processed from a list totalling around 500), after five minutes it will match around 100 e-mails, but after twenty it will only have matched around 125, having slowed down to the point that it takes nearly a minute to process each e-mail, and continues to get worse until the script eventually finishes after about three hours.

Here's a simplified version of the script:

set ignoreTheseMailboxes to { "Inbox", "Drafts", "Junk", "Sent", "Trash" }
set expiryDate to (current date) - (45 * days) as date

set myLogFile to open for access POSIX file "/Users/username/Desktop/test.log" with write permission

tell application "Mail"
    set theAccounts to every account
    repeat with eachAccount in theAccounts
        set theMailboxes to mailboxes in eachAccount
        write name of eachAccount & return to myLogFile

        repeat with eachMailbox in theMailboxes
            if (ignoreTheseMailboxes does not contain (name of eachMailbox) then
                write " " & name of eachMailbox & return to myLogFile

                set theMessages to messages in eachMailbox
                repeat with eachMessage in theMessages
                    if date received of eachMessage <= expiryDate then
                        write "     " & subject of eachMessage & return to myLogFile
                    end if
                end repeat
            end if
        end repeat
    end repeat
end tell

close myLogFile

You'll have to forgive any typos from me slimming things down, but the above is the basic behaviour of the script, and exhibits the same problem when run across the same e-mail accounts and messages.

What I'd like to know is, what is the reason that a script like this would experience increasing slowdowns, and is there a way to optimise it to avoid these kinds of issues?

It's just unfortunate that we can't see smart folders in Mail, as it could significantly reduce the number of messages needing to be searched (and avoid the need to test them).

[edit]
Also, I should note that during operation the process running the script will creep up quickly to 100% CPU usage, so AppleScript is definitely doing something, I just can't figure out what!

Best Answer

It seems the issue isn't with AppleScript, but with Script Editor, after confirming it with a back and forth with an Apple engineer. I'm still not convinced of why this should be the case, but the script runs a lot better when run via osascript, especially after refactoring it to use a whose statement when fetching messages, rather than iterating over them with an if condition.

Related Question