【问题标题】:Applescript to forward mail with attachments?Applescript 转发带有附件的邮件?
【发布时间】:2013-09-25 01:21:54
【问题描述】:

我正在尝试创建一个苹果脚本,它将接收与特定过滤器 YYYY 匹配的消息,并将它们转发到 Evernote (XXXX) 中的特定笔记本,附件完好无损。到目前为止,我已经得到这个脚本来处理转发到 Evernote 笔记本的事情,但我似乎无法让它正确处理附件。

如何将过滤器找到的电子邮件中的所有附件(理想情况下会在收到邮件时运行)附加到新转发的电子邮件中?

using terms from application "Mail"
    on perform mail action with messages selection for rule YYYY
        tell application "Mail"
            set theSelection to selection
            set thesubject to subject of item 1 of theSelection
            set theAttachments to every attachment of content of theSelection
            set theForwardedMessage to forward (item 1 of theSelection) with opening window

            tell theForwardedMessage
                make new to recipient at end of to recipients with properties {address:"ZZZZ"}

                set subject to subject of item 1 of theSelection & " @XXXX"
                repeat with a from 1 to length of theAttachments
                    log "message_attachment =" & a
                    make new attacment with properties {filename:a} at after last paragraph
                end repeat
                send
            end tell
        end tell
    end perform mail action with messages
end using terms from

【问题讨论】:

  • 我能问一下您为什么不只使用普通的 Mail.app 规则来转发邮件。没用过,但我认为它会通过电子邮件转发附件。
  • 我的博客上也有一些要求。我发现您是否想要一封电子邮件中的附件作为新草稿。他们必须先在某个地方得救。引用自我的博客。 “在您的脚本中,无法获取附件的路径。脚本的“{file name:theAttachment}”部分需要这些路径。因为原始电子邮件中的附件路径在某种程度上不存在我们可以达到的。(反正我没有找到)”
  • 我没有使用正常的邮件转发规则,因为我希望能够更改邮件的主题以便归档到印象笔记笔记本(通过主题中的@notebook)。跨度>

标签: applescript


【解决方案1】:

我已经把我不久前为某人写的脚本挖出来了。

它适用于 Mail.app 中选定的电子邮件,将附件保存在用户临时文件夹中。 然后将它们添加到您的新电子邮件中。正如我所说,您不能 AFAIK 只是将附件从原始电子邮件中拉出并放入新电子邮件中。

将它转换回邮件规则对您来说应该不难。

无论如何,你都会在脚本中更改一些明显的事情。就像您希望它发送而不是打开和显示新电子邮件一样。以及身体、主题等。

    #copyright Mark Hunte 2013
--using terms from application "Mail"
--on perform mail action with messages theMessages for rule theRule


-- set up the attachment folder path

set folderName to "temp"
set homePath to (path to temporary items from user domain) as text

set attachmentsFolder to (homePath & folderName) as text


tell application "Mail"
    set theMessages to (get selection)
    repeat with eachMessage in theMessages
        delay 0.5

        -- set up the folder name for this mail message's attachments. We use the time stamp of the  date received time stamp

        set timeStamp to (do shell script "date +%Y_%m_%d_%H%M%S")

        set attachCount to count of (mail attachments of eachMessage)
        if attachCount is not equal to 0 then
            -- use the unix /bin/test command to test if the timeStamp folder  exists. if not then create it and any intermediate directories as required
            if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & timeStamp) & " ; echo $?") is "1" then
                -- 1 is false
                do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & timeStamp)

            end if
            set attachList to {}
            try
                -- Save the attachment

                #SET replaceBadCharsWithUnderscoreInFileNames  TO TRUE  IF YOU WANT BAD CHARACTERS REMOVED FROM THE FILE NAMES THAT MAY STOP THE FILES SAVING DOWN
                set replaceBadCharsWithUnderscoreInFileNames to true
                ####

                repeat with theAttachment in eachMessage's mail attachments
                    if replaceBadCharsWithUnderscoreInFileNames then
                        set originalName to my replaceBadChars((name of theAttachment as string))

                    else
                        set originalName to (name of theAttachment as string)

                    end if



                    set savePath to attachmentsFolder & ":" & timeStamp & ":" & originalName
                    try
                        save theAttachment in file (savePath)
                        copy (savePath) to end of attachList
                    end try
                end repeat

                --on error msg
                --display dialog msg
            end try
            set theSubject to "forwarded email"
            set theBody to " body of email "
            set theAddress to "youeEmail@me.com"


            set visable to true

            set theForwardedMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
            tell theForwardedMessage
                set visible to true
                make new to recipient at end of to recipients with properties {address:theAddress} without quote original message

                tell content
                    repeat with i from 1 to the count of attachList
                        set this_file to (item i of attachList as alias)

                        make new attachment with properties {file name:this_file} at after the last paragraph
                    end repeat

                end tell

            end tell



        end if
        delay 0.5
    end repeat

end tell
--end perform mail action with messages
--end using terms from
on replaceBadChars(TEXT_)
    --log TEXT_
    set OkChars to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", ".", "(", ")", "-", ",", space}
    set TEXT_ to characters of TEXT_
    repeat with i from 1 to number of items in TEXT_
        set this_char to item i of TEXT_
        if this_char is not in OkChars then
            set item i of TEXT_ to "_"
        else

        end if
    end repeat
    set TEXT_ to TEXT_ as string

    do shell script " echo " & quoted form of TEXT_
end replaceBadChars

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多