2011-11-16 68 views
0

我正在嘗試編寫一個執行以下任務的腳本:它會通過郵箱中的所有電子郵件,找到在其主題中包含「法語」一詞的電子郵件然後將這些電子郵件的所有主題行復制到文本文件中。以下是我想出了過濾收件箱中電子郵件主題行的Applescript

tell application "TextEdit" 
    make new document 
end tell 

tell application "Mail" 
    tell the mailbox "Inbox" of account "[email protected]" 
     set numm to count of messages 
      repeat with kk from 1 to numm 
       set wordsub to subject of the message kk 
       tell application "TextEdit" 
        if "French" is in wordsub then 
         set paragraph kk of front document to wordsub & return 
        end if 
       end tell 
      end repeat 
    end tell 
end tell 

不幸的是,我不斷收到錯誤

"TextEdit got an error: The index of the event is too large to be valid."

,我已經花了幾個小時試圖修復它沒有取得多大成功。你可以看看我的代碼,看看它有什麼問題嗎?

回答

1

您的主要問題是TextEdit中的段落數量和電子郵件的數量與對方無關,所以如果您指望消息的數量,那麼TextEdit將無法理解它。例如,你可能有50條消息,但TextEdit沒有50段,所以它的錯誤。因此,我們只爲TextEdit使用單獨的計數器。

我做了其他更改。我經常通過在另一個「告訴應用程序」代碼塊中看到錯誤,所以我將它們分開。還要注意,任何「tell應用程序」塊中的唯一代碼只是該應用程序需要處理的內容。這也避免了錯誤。編程時這些都是很好的習慣。

因此試試這個...

set searchWord to "French" 
set emailAddress to "[email protected]" 

tell application "Mail" 
    set theSubjects to subject of messages of mailbox "INBOX" of account emailAddress 
end tell 

set paraCounter to 1 
repeat with i from 1 to count of theSubjects 
    set thisSubject to item i of theSubjects 
    if thisSubject contains searchWord then 
     tell application "TextEdit" 
      set paragraph paraCounter of front document to thisSubject & return 
     end tell 
     set paraCounter to paraCounter + 1 
    end if 
end repeat 
+0

非常感謝,也爲解釋。我只是測試它,它的工作原理! – user1227

相關問題