如何使用Applescript从Outlook解析HTML电子邮件内容

时间:2014-11-14 23:41:02

标签: html outlook applescript

我正在尝试解析邮箱中所有邮件的信息,其中一些邮件是HTML和其他简单文本。我可以干净地获取发件人,日期和主题,但HTML邮件只返回邮件的HTML标题部分而不是内容。

我尝试过使用ktxt类,但这对我没用。

任何指针?

tell application "Microsoft Excel"
    set LinkRemoval to make new workbook
    set theSheet to active sheet of LinkRemoval
    set formula of range "D1" of theSheet to "Message"
    set formula of range "C1" of theSheet to "Subject"
    set formula of range "B1" of theSheet to "From"
    set formula of range "A1" of theSheet to "Date"
end tell

set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles

tell application "Microsoft Outlook"
    set theRow to 2
    set introTag to "<body"
    --  THIS IS WHERE YOU SET THE MAILBOX.
    --e.g. in this example my mails is a subfolder of inbox
    set escfolder to folder "my mails" of inbox
    set theMessages to messages of escfolder
    repeat with aMessage in theMessages
        my SetDate(time received of aMessage, theRow, theSheet)
        my SetFrom(sender of aMessage, theRow, theSheet)
        my SetSubject(subject of aMessage, theRow, theSheet)
        set theContent to content of aMessage
        set existTag to introTag is in theContent
        -- true
        set p to «class ktxt» of ((content of aMessage as text) as record)
        if existTag then
            --set ex to my extractBetween(p, "<body>", "</body>")
            set ex to my remove_markup(p)
            my SetMessage(ex, theRow, theSheet)

        else
            -- false
            my SetMessage(p, theRow, theSheet)
        end if
        set theRow to theRow + 1
    end repeat
end tell

on SetDate(theDate, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "A" & theRow
        set formula of range theRange of theSheet to theDate
    end tell
end SetDate

on SetFrom(theSender, theRow, theSheet)
    try
        set theSenderName to name of theSender
    on error
        set theSenderName to address of theSender
    end try
    tell application "Microsoft Excel"
        set theRange to "B" & theRow
        set formula of range theRange of theSheet to theSenderName
    end tell
end SetFrom

on SetSubject(theSubject, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "C" & theRow
        set formula of range theRange of theSheet to theSubject
    end tell
end SetSubject

on SetMessage(theMessage, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "D" & theRow
        set formula of range theRange of theSheet to theMessage
    end tell
end SetMessage

on remove_markup(this_text)
    set copy_flag to true
    set the clean_text to ""
    repeat with this_char in this_text
        set this_char to the contents of this_char
        if this_char is "<" then
            set the copy_flag to false
        else if this_char is ">" then
            set the copy_flag to true
        else if the copy_flag is true then
            set the clean_text to the clean_text & this_char as string
        end if
    end repeat
    return the clean_text
end remove_markup

on extractBetween(SearchText, startText1, endText)
    return SearchText
    --set tid to AppleScript's text item delimiters
    --set AppleScript's text item delimiters to startText1
    --set endItems to text item -1 of SearchText
    --set AppleScript's text item delimiters to endText
    --set beginningToEnd to text item 1 of endItems
    --set finalText to (text items 2 thru -1 of beginningToEnd) as text
    --set AppleScript's text item delimiters to tid
    --return finalText
end extractBetween

0 个答案:

没有答案