AppleScript / FolderAction - 在文件夹中删除时重命名文件>无尽的循环

时间:2017-06-16 21:16:13

标签: macos applescript

我正在尝试使用FolderAction和AppleScript执行以下操作:

每次我将文件放在特定文件夹上时,都应将其重命名,然后移到另一个文件夹中。

问题是我得到类似无限循环(我认为)的东西,因为当文件被重命名时,文件夹假定文件夹中有一个新文件,依此类推。

我真的不知道如何避免这种情况并停止无休止的循环。这是我的剧本:

global newName
set newName to ""

on adding folder items to theAttachedFolder after receiving theNewItems
    -- Get the name of the attached folder
    tell application "Finder"
        set theName to name of theAttachedFolder

        -- Count the new items
        set theCount to length of theNewItems

        -- Display an alert indicating that the new items were received
        activate

        -- Loop through the newly detected items
        repeat with anItem in theNewItems

            set oldFileName to name of anItem

            -- Rename the file
            set the name of anItem to "NewFile" & oldFileName

            -- Move the file to other folder
            move anItem to "Macintosh HD:Users:blabla:blabla"

        end repeat
    end tell

    tell application "Finder"
        delete files of folder "Macintosh HD:Users:user:thisfolder
    end tell

end adding folder items to

1 个答案:

答案 0 :(得分:0)

你是对的,重命名文件会再次触发文件夹操作。

解决方案是更改顺序:首先移动然后重命名。

        -- Move the file to other folder
        set movedItem to move anItem to "Macintosh HD:Users:blabla:blabla"

       -- Rename the file
        set the name of movedItem to "NewFile" & oldFileName