Applescript:从邮件解析

时间:2012-10-10 15:28:23

标签: applescript

我正在寻找一个通过我的邮件获取所需结果的AppleScript:

电子邮件主题:Registrierung fuer die Badener Hochzeitstage - 2587(这里我需要号码)

我需要的电子邮件内容是“Frau / Herr / Firma”之后的名称,直到下一个“空格”

我的php / mysql代码出错了我在我的数据库中没有这个,所以我需要解析大约400个电子邮件。

tell application "Mail"

set theMessages to message 1 of mailbox "INBOX" of account "Die Badenerhochzeitstage" whose subject begins with "Registrierung fuer"
set theContent to content of theMessages

set someData to paragraphs of theContent
repeat with someData in theContent

    if someData begins with "Firma" then
        set theURL to paragraph of someData
    else
        set theURL to paragraph 10 of theContent -- this works but this line can change weekly
    end if
end repeat

end tell

在这里,我尝试过,但结果我只是得到一些线而不是我想要的。

2 个答案:

答案 0 :(得分:0)

试试这个:

   set TID to text item delimiters
set myRecords to {}

set outputFile to quoted form of (POSIX path of ((path to desktop as text) & "LaunchAgent_Alert.txt")) -- represents "MacHD:Users:david:Desktop:result.txt"

tell application "Mail" to set theMessages to every message of mailbox "INBOX" of account "Badener Hochzeitstage" whose subject begins with "Registrierung fuer"

repeat with aMessage in theMessages
   tell application "Mail" to set {mSubject, mContent} to {subject, content} of aMessage
   set AppleScript's text item delimiters to "-"
   set mSubject to text item 2 of mSubject

   set AppleScript's text item delimiters to "Herr/Frau/Firma "
   set mContent to first word of (text item 2 of mContent)

   set end of myRecords to {mSubject & return & mContent & return & return}

end repeat

set myRecords to myRecords as text
do shell script "echo " & myRecords & " > " & outputFile

set text item delimiters to TID

答案 1 :(得分:0)

试试这个......

set theAccount to "Die Badenerhochzeitstage"
set subjectText to "Registrierung fuer"
set subjectSearch to "-"
set contentSearch to "Frau/Herr/Firma"
set theResults to {}

tell application "Mail"
    try
        set theMessages to messages of mailbox "INBOX" of account theAccount whose subject begins with subjectText
        if theMessages is {} then
            display dialog "No Messages were found which begins with:" & return & subjectText buttons {"OK"} default button 1
            return
        end if

        repeat with aMessage in theMessages
            tell aMessage
                set theSubject to subject
                set theContent to content
            end tell
            set theNumber to my getFirstWordAfterSearchText(subjectSearch, theSubject)
            set theWord to my getFirstWordAfterSearchText(contentSearch, theContent)
            set end of theResults to {theNumber, theWord}
        end repeat
    on error theError
        display dialog theError buttons {"OK"} default button 1
        return
    end try
end tell
return theResults

(*============== SUBROUTINES =================*)
on getFirstWordAfterSearchText(searchString, theText)
    try
        set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, searchString}
        set textItems to text items of theText
        set AppleScript's text item delimiters to tids
        return (first word of (item 2 of textItems))
    on error theError
        return ""
    end try
end getFirstWordAfterSearchText