0

私はAppleScriptを実行して、上司の受信トレイにあるすべてのメッセージからすべての電子メールアドレスを抽出していますが、上司のコンピューターでフリーズし、私の場合は正常に機能します。

私のコンピューターはメール4.6でSnowleopardを実行しており、違いがあれば彼はメール5.3でLionを実行しています。

また、私は通常メールを使用せず、スクリプトをテストするためにそれらのメッセージのみを取得し、彼のメール数は60 000を超えているため、受信トレイには約400通のメールしかありません。

スクリプトは約20秒で私の電子メールを実行し、彼は40を実行するのに2分かかり、その後フリーズしました。

彼の上位バージョンでコードがフリーズする原因となる可能性のあるコードに何か問題があるのか​​、または存在する電子メールの増加が原因であるのか疑問に思いました。

別の注意点として、私がこれを適応させたスクリプトは、ファイルに書き込む前にアドレスを並べ替えて重複を削除することであったため、それらをすべて1つずつ書き込むことはおそらく逆効果であることを知っていますが、メールの数が多いため、プロセスを高速化し、書き込みに使用するメモリを減らします。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
4

1 に答える 1

3

編集:さらに考えた後、私は投稿した最初のスクリプトを削除しました。私の考えでは、あなたが見ている問題は、この行で一度に60,000以上の電子メールを受信して​​いるためです...

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

したがって、アイデアは一度にたくさんを取得することです。変数writeEveryXMessagesを使用して、一度に500個のメッセージを取得するように指定し、各ループで、終了するまで次の500個のメッセージを取得します。

注:コードをもう少し効率的に変更し、考えられるいくつかのバグを修正しました。たとえば、writeコマンドがコードのMailtellブロックに含まれなくなりました。また、500個のメッセージを一度にファイルに書き込むようになりました。このスクリプトは、MountainLionおよびMailv6.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
于 2013-01-21T22:14:06.140 に答える