从邮件中提取电子邮件时,Applescript会冻结

时间:2013-01-21 16:26:14

标签: email applescript

我正在运行一个AppleScript来从我老板的收件箱中的所有邮件中提取所有邮件地址,并且它在他的计算机上冻结并在我的网站上正常工作。

我的计算机正在运行带有邮件4.6的Snow leopard,如果这有任何不同,他正在使用邮件运行Lion。

此外,我的收件箱只有大约400封邮件,因为我通常不使用邮件,只收到这些邮件来测试脚本,而且他的邮件超过6万。

脚本在大约20秒内完成了我的电子邮件,然后他花了2分钟做了40分钟然后冻结了。

我想知道代码是否有任何问题可能导致其在更高版本中冻结或由于电子邮件的增加而存在。

另一方面,我知道一个接一个地写它们可能会适得其反,因为我改编的脚本是在将地址排序并删除重复之前将其写入文件但我认为由于数量很多邮件说它会加快进程并使用更少的内存来编写它们。 PLus计数器有助于知道脚本的位置。

这是代码:

tell application "Finder" to set ptd to path to documents folder as string
    set theFile to ptd & "extracted3.txt"
    set theFileID to open for access theFile with write permission

set counter to 0

tell application "Mail"
    set selectionMessage to selection -- just select the first message in the folder
    set thisMessage to item 1 of selectionMessage
    set theseMessages to (every message in (mailbox of thisMessage))
    repeat with eachMessage in theseMessages
        try
            set counter to counter + 1
            set theFrom to (extract address from sender of eachMessage)
            set theFromName to (extract name from sender of eachMessage)
            set theFromTemp to theFrom & "," & theFromName & "," & counter
            write theFromTemp & return to theFileID as «class utf8»
            if (address of to recipient) of eachMessage is not {} then
                repeat with i from 1 to count of to recipient of eachMessage
                    set theTo to (address of to recipient i) of eachMessage as string
                    set theToName to (name of to recipient i) of eachMessage as string
                    set theToTemp to theTo & "," & theToName & "," & counter
                    write theToTemp & return to theFileID as «class utf8»
                end repeat
            end if
            if (address of cc recipient) of eachMessage is not {} then
                repeat with i from 1 to count of cc recipient of eachMessage
                    set theCC to (address of cc recipient i) of eachMessage as string
                    set theCCName to (name of cc recipient i) of eachMessage as string
                    set theCCTemp to theCC & "," & theCCName & "," & counter
                    write theCCTemp & return to theFileID as «class utf8»
                end repeat
            end if
        end try
    end repeat
end tell

close access theFileID

1 个答案:

答案 0 :(得分:3)

编辑 :经过深思熟虑,我删除了我发布的第一个脚本。我的想法是你看到的问题是因为你在这一行中同时收到了60,000多封电子邮件......

set theseMessages to (every message in (mailbox of thisMessage))

所以我的想法是一次只得到一堆。我使用变量writeEveryXMessages来指定您一次应该获得500条消息,并且在每个循环中我们获得接下来的500条消息,直到完成。

注意:我修改了你的代码以提高效率并修复了一些可能的错误,例如写命令不再出现在Mail tell代码块中。此外,它现在一次将这500条消息写入文件。此脚本适用于Mountain Lion和Mail v6.2。它也适合你。

我希望这可以解决您的问题!祝你好运。

set theFile to (path to documents folder as text) & "extracted3.txt"
set writeEveryXMessages to 500
set counter to 1

try
    set theFileID to open for access file theFile with write permission

    tell application "Mail"
        set selectedMessage to item 1 of (get selection)
        set theMailbox to mailbox of selectedMessage
        set messageCount to count of messages in theMailbox
    end tell

    repeat
        set endCount to counter + writeEveryXMessages
        if endCount is greater than messageCount then set endCount to messageCount
        set theString to ""

        tell application "Mail"
            set theseMessages to messages counter thru endCount of theMailbox
        end tell

        repeat with eachMessage in theseMessages
            set theFromTemp to ""
            set theToTemp to ""
            set theCCTemp to ""

            try
                tell application "Mail"
                    tell eachMessage
                        set theSender to sender
                        set toRecipients to to recipients
                        set ccRecipients to cc recipients
                    end tell

                    set theFrom to extract address from theSender
                    set theFromName to extract name from theSender
                    set theFromTemp to "From: " & theFrom & "," & theFromName & "," & counter & return

                    if toRecipients is not {} then
                        repeat with toRecipient in toRecipients
                            try
                                set theTo to address of toRecipient
                                set theToName to name of toRecipient
                                set theToTemp to theToTemp & "  To: " & theTo & "," & theToName & "," & counter & return
                            end try
                        end repeat
                    end if

                    if ccRecipients is not {} then
                        repeat with ccRecipient in ccRecipients
                            try
                                set theCC to address of ccRecipient
                                set theCCName to name of ccRecipient
                                set theCCTemp to theCCTemp & "  CC: " & theCC & "," & theCCName & "," & counter & return
                            end try
                        end repeat
                    end if
                end tell

                set theString to theString & theFromTemp & theToTemp & theCCTemp & return
            end try

            set counter to counter + 1
        end repeat

        write theString to theFileID as «class utf8»
        if counter is greater than or equal to messageCount then
            set theString to ""
            exit repeat
        end if
    end repeat

    close access theFileID
on error theError
    log theError
    try
        close access file theFile
    end try
end try