【问题标题】:Applescript: on adding folder items to; renaming files without the loopApplescript:将文件夹项目添加到;在没有循环的情况下重命名文件
【发布时间】:2018-01-17 01:00:30
【问题描述】:

我创建了一个 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 

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

【问题讨论】:

    标签: applescript directory finder


    【解决方案1】:

    脚本被调用了两次,因为重命名监视文件夹中的文件——出于所有意图和目的——就像在文件夹中添加一个新文件一样。因此,在实际添加文件时调用一次;并在重命名时再次调用。

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

    作为旁注,我注意到您有一个 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 file este_file to folder "BetterinTexas" of desktop或更短的move file este_file to folder "BetterinTexas"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多