简体   繁体   English

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

[英]Applescript freezes when extracting email from mail

I'm running an applescript to extract all the email adress from all the messages in my boss's inbox and it freezes on his computer and works fine on mine. 我正在运行一个applescript,以从老板收件箱中的所有邮件中提取所有电子邮件地址,该邮件冻结在他的计算机上,并且在我的计算机上运行良好。

my computer is running Snow leopard with mail 4.6 and his is running Lion with mail 5.3 if that makes any difference. 我的计算机运行的是Snow豹,邮件为4.6,而他的运行的Lion是邮件,邮件为5.3(如果有任何区别)。

Also my inbox only has around 400 mails since i don't usually use mail and only got those messages to test the script and his has over 60 000. 另外,我的收件箱只有大约400封邮件,因为我通常不使用邮件,只收到这些邮件来测试脚本,他的收件箱已超过6万。

The script ran through my email in around 20 seconds and on his took 2 minutes to do 40 then froze. 脚本在大约20秒内遍历了我的电子邮件,他花了2分钟完成40次操作,然后冻结了。

I was wondering if there was anything wrong with the code that could cause it to freeze in his higher version or due to the increase in email present. 我想知道代码中是否有任何错误,可能导致其冻结在他的更高版本中或由于电子邮件的增加而导致。

On another note i know that writing them all one by one is probably counter-productive because the script i adapted this from was sorting the adresses and removing duplicates before writing them to the file but i thought that due to the large number of mails that it would speed the process and use less memory to just write them. 在另一个注意事项上,我知道将它们全部一一写入可能会适得其反,因为我改编的脚本是在将地址写入文件之前对地址进行排序并删除重复项,但是我认为由于邮件数量众多这样可以加快处理速度,并减少写入文件所需的内存。 PLus the counters helps to know where the script is at. PLus计数器有助于了解脚本的位置。

here is the code : 这是代码:

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

EDIT : after further thought, I removed the first script I posted. 编辑 :经过进一步考虑,我删除了我发布的第一个脚本。 My thought is the problem you are seeing is because you are getting 60,000+ emails at once in this line... 我的想法是,您看到的问题是因为您在该行中一次收到60,000多封电子邮件...

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

So the idea is to just get a bunch at a time. 因此,想法是一次只得到一堆。 I use the variable writeEveryXMessages to specify that you should get 500 messages at a time, and on each loop we get the next 500 until finished. 我使用变量writeEveryXMessages来指定您一次应获取500条消息,并且在每个循环中,我们将获取下500条消息,直到完成。

NOTE: I modified your code to be a little more efficient and fixed a few possible bugs, for example the write command is no longer in the Mail tell block of code. 注意:我将您的代码进行了修改,使其效率更高一些,并修复了一些可能的错误,例如,在“告诉”代码块中不再使用write命令。 Also it now writes those 500 messages to file at one time. 现在它还会一次将这500条消息写入文件。 This script works on Mountain Lion and Mail v6.2. 该脚本适用于Mountain Lion and Mail v6.2。 It should work for you too. 它也应该为您工作。

I hope this fixes your problem! 我希望这可以解决您的问题! Good luck. 祝好运。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM