2011-11-16 66 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

相关问题