【问题标题】:AppleScript / FolderAction - Rename files when dropped in folder > Endless LoopAppleScript / FolderAction - 在文件夹中重命名文件> Endless Loop
【发布时间】:2017-06-17 11:57:40
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

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

    解决办法是改变顺序:先移动再重命名。

            -- 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多