Applescript:将文件夹项目添加到;不循环重命名文件

Applescript: on adding folder items to; renaming files without the loop

我创建了一个 Droplet,它允许我从输入框中重命名文件。

on adding folder items to este_folder after receiving este_file


    display dialog "what's their name?" default answer ""

    set text_returned to text returned of the result & ".jpg"

    display dialog text_returned

    tell application "Finder"
        set the name of file este_file to text_returned
    end tell

end adding folder items to

它工作正常,但它创建了一个循环,我必须再次点击取消才能停止脚本,因为它认为已经添加了一个新文件。我只想重命名一次;然后不会再弹出第二个对话框。我已尝试将文件重新路由到另一个文件夹:

on adding folder items to este_folder after receiving este_file
    display dialog "what's their name?" default answer ""

        set text_returned to text returned of the result & ".jpg"

        display dialog text_returned

        tell application "Finder"
            set the name of file este_file to text_returned
        end tell
    repeat with anItem in este_file
            tell application "Finder"
                 set destFolder to "Macintosh HD:Users:maxwellanderson:Desktop:BetterinTexas" as alias
                move anItem to folder destFolder
            end tell
        end repeat
end adding folder items to 

但这也不起作用——它不处理脚本的重命名部分。关于我应该如何摆脱第二个对话框的任何建议?

脚本被调用两次,因为重命名监视文件夹中的文件——就所有意图和目的而言——就像将新文件添加到文件夹中一样。因此,在实际添加文件时调用一次;并在重命名时第二次调用。

按照您的建议移动文件是可行的,但是您必须先移动文件,然后 重命名它。因此,将移动文件的代码放在脚本顶部附近,然后是底部的重命名位。

作为旁注,我注意到您有一个 repeat with 循环来处理多个文件移动,但只有一个语句处理单个文件重命名。其中一个与另一个不同。如果这个监视文件夹同时收到多个文件,它很可能会将它们全部重命名为相同的名称,从而可能覆盖多个文件。如果监视文件夹一次只接收一个文件,无论如何,那么 repeat with 循环是多余的。

此代码以您的代码为蓝本,将处理单个文件的移动和重命名(但不是一组文件——或者,更准确地说,正如我上面所说的,它将多个文件重命名为相同的名称,因此覆盖列表中除最后一个之外的所有内容):

    on adding folder items to este_folder after receiving este_file

        set destFolder to POSIX file "/Users/maxwellanderson/Desktop/BetterinTexas" as alias

        set text_returned to text returned of ¬
            (display dialog "what's their name?" default answer "") ¬
                & ".jpg"

        display dialog text_returned

        tell application "Finder" to ¬
            set the name of ¬
                (move file este_file to destFolder) ¬
                    to text_returned

    end adding folder items to

如果您需要它来处理多个文件,那么您可以将 set text_returnedto text_returned 的所有内容包装在 repeat with 循环中,就像您在第二个代码块中所做的那样。这将依次打开对话框(每个文件一个)和相应的 move/rename 个文件。

如果您有任何问题或需要说明,请发表评论,我会尽快回复您。