想要使用 AppleScript 创建一个目录(如果它不存在)

Want to create a directory with AppleScript if it doesn't already exist

我想自动将邮件中的附件保存到我的硬盘上。经过大量研究后,我在 Mail 中设置了一个规则,每当收到带有附件的电子邮件时运行下面的脚本。

use scripting additions

using terms from application "Mail"
    on perform mail action with messages messageList for rule aRule
        set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
        tell application "Mail"
            repeat with aMessage in messageList
                repeat with anAttachment in mail attachments of aMessage
                    set senderName to (extract name from sender of aMessage)
                    set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
                    set timeStamp to (d & "/" & (m as integer) & "/" & y) as string
                    set attachmentName to senderName & " - " & timeStamp & " - " & name of anAttachment
                    
                    set doSave to true
                    set originalName to name of anAttachment
                    if originalName contains "jpg" then
                        set doSave to false
                    else if originalName contains "jpeg" then
                        set doSave to false
                    else if originalName contains "gif" then
                        set doSave to false
                    else if originalName contains "png" then
                        set doSave to false
                    else if originalName contains "html" then
                        set doSave to false
                    end if
                    
                    if doSave is true then save anAttachment in file (destinationPath & attachmentName)
                    
                end repeat
            end repeat
        end tell
        
    end perform mail action with messages
end using terms from

这很好用。排除项会阻止脚本在电子邮件签名等中保存徽标。 但是这个脚本确实意味着所有文件都在一个目录中(电子邮件附件)。

因为脚本有发件人的名字,我一直在尝试做的是使用发件人的名字在 'Email Attachments' 中创建一个子目录(如果它不存在则只创建它)然后保存该目录中附件的 'timestamp & name' 文件。

这样我将在 'Email Attachments' 下有一个目录结构,所有发件人姓名及其附件都在自己的目录中。

我已经尽我所能 - 但没有成功(我可以 post 我最近的尝试在这里,但宁愿保留我的脸红!!)

有谁知道如何修改我的脚本以使其工作???如果您有任何想法,谢谢...

测试目标文件夹是否存在,如果不存在,可以使用下面的example AppleScript 代码:

tell application "System Events"
    if not (exists folder (destinationPath & senderName)) then
        make new folder at end of alias destinationPath with properties {name:senderName}
    end if
end tell

另一种创建 文件夹 的方法是:

do shell script "mkdir -p " & (POSIX path of destinationPath & senderName)'s quoted form

如果它不存在,这将创建它,如果存在,则什么也不做。


附带说明一下,这里有一种方法可以消除 code[= 的 if ... else if block 46=] 你有:

set doSave to true
set originalName to name of anAttachment
set doNotSaveFileTypeList to {"jpg", "jpeg", "gif", "png", "html"}
set AppleScript's text item delimiters to "."
set fileExtension to last text item of originalName
set AppleScript's text item delimiters to ""
if the doNotSaveFileTypeList contains fileExtension then
    set doSave to false
end if

action 对于所有 list items 相同时,我喜欢使用这种方法,因为它更容易添加或删除list 而无需修改 代码块 .

的其余部分

注意:示例 AppleScript code 就是这样,不包含任何额外的 错误处理 可能是适当的。用户有责任添加任何 错误处理 可能是适当的、需要的或想要的。