Applescript将电子邮件从MS Outlook 2019转移到DevonThink

时间:2019-05-29 10:50:20

标签: applescript

在等待与此相关的帮助时。我成功创建了一个可以满足我需求的脚本。所以也许其他人会想要。 我想运行一个自动化脚本,以将Outlook中的传入电子邮件移动到DevonThink。 从一些示例中编写了一个脚本,它就可以工作。

我刚刚在Outlook中创建了一个新文件夹“ DEVON”。 具有将所有新邮件复制到此文件夹的标尺。 比脚本每5分钟运行一次。将消息传输到DEVON。 同时添加一些标签和过滤器等:) 并从DEVON文件夹中删除该消息。 有效。


-- this string is used when the message subject is empty
property pNoSubjectString : "(no subject)"

-- Some initial variables
set today to (current date)
set cutoff to 1 * days
set shortCutoff to 3 * hours

tell application "Microsoft Outlook"
    try
        set myInbox to folder "Inbox" of default account
        set DEVONfolder to mail folder "DEVON" of myInbox
        set theMessages to messages of DEVONfolder

        # A couple of counters used for logging changes
        set countMoved to 0
        -- set countDeleted to 0
        tell application id "DNtp"
            if not (exists current database) then error "No database is in use."
            set theGroup to preferred import destination
        end tell

        repeat with theMessage in theMessages
            try
                set theSubject to subject of theMessage
                set theSender to sender of theMessage
                set theSender to (address of theSender) as string
                set theSource to source of theMessage
                set theDateReceived to time received of theMessage
                set theDateSent to time sent of theMessage

                -- set messageTime to time received of theMessage
                set messageAge to today - theDateReceived
                -- set theSender to sender of theMessage
                -- set fromAddress to address of theSender
                set isinvite to is meeting of theMessage


                # Start of mail specific rules

                # This moves to devonthink if age is less than cutoff
                if isinvite is not true then
                    if theSubject is equal to "" then set theSubject to pNoSubjectString

                    set theCategories to {}
                    set theList to (category of theMessage)
                    repeat with theCategory in theList
                        set theCategories to theCategories & (name of theCategory)
                    end repeat
                    set isFlagged to true
                    if todo flag of theMessage is (not flagged) then set isFlagged to false
                    set isUnread to is read of theMessage

                    tell application id "DNtp"
                        set theRecord to create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string)} in theGroup
                        if theCategories is not {} then
                            set theTags to tags of theRecord
                            set theTags to theTags & theCategories
                            set tags of theRecord to theTags
                            -- add tags logic:




                        end if
                        if isFlagged then set state of theRecord to true
                        if isUnread then set unread of theRecord to true
                    end tell
                    set countMoved to countMoved + 1
                end if


                #           # This deletes a CI notification if it is more than 3 hours old or has been read
                #           if fromAddress is "semaphore+notifications@renderedtext.com" then
                #               if messageAge > shortCutoff or (is read) of theMessage is true then
                #                   delete theMessage
                #                   set countDeleted to countDeleted + 1
                #               end if
                #           end if

                #           # This archives a New Relic alert if it's more than 2 days old
                #           if name of theSender contains "New Relic Alert" then
                #               if messageAge > cutoff then
                #                   move theMessage to folder "Notifications" of archive
                #                   set countMoved to countMoved + 1
                #               end if
                #           end if

                # and so on for every rule

                # End of mail specific rules

            on error errorMsg
                log "Error: " & errorMsg
            end try

            --Delete the transfered message
            -- permanently delete theMessages

        end repeat
    on error error_message number error_number
        if the error_number is not -128 then display alert "Outlook" message error_message as warning
    end try

    -- log "Outlook cleanup ran at " & today & " - " & countMoved & " moved, " & countDeleted & " deleted"
end tell

tell application "Microsoft Outlook"
    try
        set myInbox to folder "Inbox" of default account
        set DEVONfolder to mail folder "DEVON" of myInbox
        set theMessages to messages of DEVONfolder
        repeat with theMessage in theMessages
        --Delete the transfered message
        permanently delete theMessage
        end repeat
    on error error_message number error_number
    if the error_number is not -128 then display alert "Outlook" message error_message as warning
end try
end tell

0 个答案:

没有答案