2017-06-16 80 views
0

我试图做一个FolderAction和AppleScript如下:的AppleScript/FolderAction - 文件夹中跌落时>重命名文件死循环

每次我滴在一个特定的文件夹中的文件应该被重命名,然后移动到另一个文件夹。

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

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

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 

回答

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