使用AppleScript

时间:2015-10-20 13:05:44

标签: sorting applescript subdirectory

我有几千个文件要分类到子文件夹......

FILENAMES :(几个不同的扩展名)

  • DK10xxx
  • DK11xxx
  • DK12xxx

使用AppleScript:

repeat with i from 10 to 99
  tell application "Finder"
  set the_folder1 to folder "Sorting" of folder "Temp" of disk "HDD"
  set the_folder2 to folder ("DK" & i) of folder "Sorting" of folder "Temp" of disk "HDD"
  move (every item of the_folder1 whose name starts with ("DK" & i)) to the_folder2
  end tell
end repeat
end run

结果:

  • 错误" Finder收到错误:AppleEvent超时。"编号-1712
  • 我必须重新启动Finder

1 个答案:

答案 0 :(得分:1)

试试这个,它会在重复循环中检查源文件夹的所有文件。

如果文件以DK开头,则会根据文件名的前4个字符创建一个文件夹,并将当前文件移动到子文件夹。

property sourceFolder : "HDD:Temp:Sorting"

tell application "Finder"
    repeat with aFile in (get files of folder sourceFolder) as alias list
        set fileName to name of aFile
        if fileName starts with "DK" then
            set prefix to text 1 thru 4 of fileName
            if not (exists folder prefix of folder sourceFolder) then
                make new folder at folder sourceFolder with properties {name:prefix}
            end if
            move aFile to folder prefix of folder sourceFolder
        end if
    end repeat
end tell

您收到的错误是超时错误。如果单个Apple Event超过2分钟,则会发生这种情况。上面的代码试图通过使用较短的Apple事件来避免此错误。

如果错误仍然发生(在重复行中)将重复循环包装到with timeout块中

with timeout of 1000000 seconds
  repeat with aFile in (get files of folder sourceFolder) as alias list
  ...
  end repeat
end timeout