macOS 和 AppleScript:将电子邮件附件保存在文件夹中并分配标签

问题描述 投票:0回答:1

我写了一个 AppleScript,我想通过 macOS 邮件应用程序中的规则运行它。 该脚本应该执行以下操作:

  1. 等待 10 秒以确保所有电子邮件都已下载
  2. 检查新邮件是否带有 PDF 附件
  3. 如果是,将它们保存到指定文件夹
  4. 如果这个文件夹不存在:创建它
  5. 为保存的 PDF 分配标签

但是它什么也没做,我就是找不到原因。我在控制台中找不到任何东西,文件夹权限似乎也没有问题。

你能帮帮我吗?

提前致谢, 朱利叶斯

delay 10
using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with theMessage in theMessages
            tell application "Mail"
                repeat with theAttachment in mail attachments of theMessage
                    set attachmentName to name of theAttachment
                    if attachmentName ends with ".pdf" then
                        set saveFolder to "/Users/username/Desktop/Aktuelles"
                        log "Save folder: " & saveFolder
                        tell application "Finder"
                            if not (exists folder saveFolder) then
                                log "Creating folder: " & saveFolder
                                make new folder at (path to desktop folder) with properties {name:"Aktuelles"}
                            end if
                            set newFile to (make new file at folder saveFolder with properties {name:attachmentName})
                            log "New file path: " & POSIX path of newFile
                            set file type of newFile to "PDF "
                            set creation date of newFile to (current date)
                            set modification date of newFile to (current date)
                            set tags of newFile to "Aktuell"
                            log "File tags: " & tags of newFile
                        end tell
                    end if
                end repeat
            end tell
        end repeat
    end perform mail action with messages
end using terms from
macos applescript
1个回答
0
投票

有很多你没有考虑到的细微差别,但是你立即尝试不畏艰险(从困难的开始)也没关系。 1) 只有 Mail.app 应用程序本身可以将附件保存到磁盘。 2) Plain AppleScript 只能以有限的方式设置标签,所以最好使用 AsObjC。 3) 标签必须已经在 Finder 设置中(如果它不是标准的,必须手动添加)。 4) 不应使用 Finder 告诉块内的脚本添加命令(Posix 路径、当前日期))。

我给你写了一个有效的脚本。但是,请记住还有一个更重要的细微差别:我的脚本不检查附件名称中是否存在禁止字符。此类文件将被忽略。如有必要,您自己可以考虑此类情况。我的脚本只是假定名称是合法的。

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

set actuellesHFS to ((path to desktop) as text) & "Aktuelles:"
tell application "Finder" to if not (exists folder actuellesHFS) then make new folder at desktop with properties {name:"Aktuelles"}
delay 10

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"
            repeat with aMessage in theMessages
                tell aMessage to set mailAttachments to its mail attachments
                if (count mailAttachments) > 0 then -- EDITED
                    tell aMessage to repeat with anAttachment in mailAttachments
                        set aName to name of anAttachment
                        if aName ends with ".pdf" then
                            save anAttachment in ((actuellesHFS & aName) as «class furl»)
                            set posixPath to POSIX path of ((actuellesHFS & aName) as alias)
                            tell me to setTags:{"Orange"} forPath:posixPath
                        end if
                    end repeat
                end if -- EDITED
            end repeat
        end tell
    end perform mail action with messages
end using terms from

on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
    set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
    aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
© www.soinside.com 2019 - 2024. All rights reserved.