【问题标题】:Want to create a directory with AppleScript if it doesn't already exist如果 AppleScript 尚不存在,则想用它创建一个目录
【发布时间】:2020-07-14 06:47:17
【问题描述】:

我想将 Mail 中的附件自动保存到我的硬盘中。经过大量研究后,我在 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

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

由于脚本具有发件人的名称,我一直在尝试使用发件人名称在“电子邮件附件”中创建一个子目录(bur 仅在不存在时创建它),然后将文件另存为该目录中附件的“时间戳和名称”。

这样我将在“电子邮件附件”下有一个目录结构,所有发件人姓名及其附件都在他们自己的目录中。

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

有谁知道如何修改我的脚本来完成这项工作???如果您有任何想法,谢谢...

【问题讨论】:

    标签: applescript posix attachment


    【解决方案1】:

    要测试目标文件夹是否存在,如果不创建,可以使用以下示例 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
    

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


    顺便说一句,这里有一种方法可以消除您拥有的代码中的if ... else if block:

    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
    

    当所有列表项的动作都相同时,我喜欢使用这种方法,因为它更容易从列表中添加或删除 无需修改代码块的其余部分。


    注意:示例 AppleScript 代码 仅此而已,不包含任何额外的错误处理 视情况而定。用户有责任根据需要或需要添加任何错误处理。

    【讨论】:

    • 谢谢,太好了 - 工作愉快并创建文件夹。我也喜欢 if/else 脚本,它可以更容易添加。你能再给我一份帮助吗?我正在尝试编辑“如果 dosave 为真,则保存....”行以使用新创建的文件夹,但我无法让它工作。有任何想法吗?谢谢
    • @Jonathan Hewitt,基于当前的代码,这对我有用:if doSave is true then save anAttachment in file (destinationPath & senderName & ":" & attachmentName) 也就是说,我不会在timeStamp 中使用/,请使用- 代替。
    • 我没有在自己的电脑上检查这个,所以也许我记错了。我不认为 SystemEvents 如果尝试在已经存在的路径上创建新文件/文件夹时会抱怨。因此,即使没有if...then 测试,也可以简单地使用:tell app "System Events" to make new folder with properties {name:destinationPath & senderName}(前提是destinationPath 的文件夹已经存在)。
    猜你喜欢
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多