简体   繁体   English

使用AppleScript根据名称将文件夹中的文件排序到子文件夹

[英]Sorting files in folder to subfolder based on name with AppleScript

I have several thousand files I want to sort into subfolders... 我有数千个文件要归类到子文件夹中。

FILENAMES: (several different extensions) 文件名:(几个不同的扩展名)

  • DK10xxx DK10xxx
  • DK11xxx DK11xxx
  • DK12xxx DK12xxx

Using AppleScript: 使用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

RESULT: 结果:

  • error "Finder got an error: AppleEvent timed out." 错误“ Finder出现错误:AppleEvent超时。” number -1712 编号-1712
  • and I have to restart Finder 我必须重新启动Finder

Try this, it checks all files of the source folder in a repeat loop. 尝试此操作,它将在重复循环中检查源文件夹的所有文件。

If a file starts with DK it creates a folder if necessary named by the first 4 characters of the file name and moves the current file to the subfolder. 如果文件以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

The error you got is a time out error. 您收到的错误是超时错误。 It occurs if a single Apple Event takes longer than 2 minutes. 如果单个Apple Event花费的时间超过2分钟,则会发生这种情况。 The code above tries to avoid this error by using shorter Apple Events. 上面的代码尝试通过使用较短的Apple Events来避免此错误。

If the error still occurs (in the repeat line) wrap the repeat loop into a with timeout block 如果错误仍然存​​在(在重复行中),则将重复循环包装到with timeout块中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM