AppleScript在将文件移动到文件夹时死得很慢

时间:2015-05-16 12:43:16

标签: applescript

我试图根据文件创建日期将一堆文件移动到文件夹中。使用下面的脚本,移动速度约为每秒1个文件。 64000个文件。 当我尝试

时脚本超时
set myFiles to items of myFolder whose name ends with "jpg"
repeat with aFile in myFiles

所以现在我按文件处理文件,只检查日期比较失败时存在的文件夹,只是稍微加快了进程。我试过了明显的

set myDayBuddies to items of myFolder whose creation date is theFileDate

但是这也没有多大帮助。我怀疑Finder会一次又一次地读取整个文件夹内容,从而产生巨大的开销。

进一步加快这个速度的正确方法是什么?在PHP中,我知道我可以逐个读取目录流中的条目。

脚本:

tell application "Finder"
    set myFolder to choose folder
    set prevDateString to ""
    repeat while true
        try
            set aFile to first item of myFolder whose name ends with "jpg"
            set theFileDate to (the creation date of aFile)
            set theDateString to my composedate(theFileDate)
            if (theDateString is not prevDateString) then
                if not (exists folder ((myFolder & theDateString) as text)) then
                    make new folder at myFolder with properties {name:theDateString}
                end if
                set prevDateString to theDateString
                set destPath to (((myFolder as text) & theDateString) as alias)
            end if
            move aFile to destPath
        on error
            return
        end try
    end repeat
end tell
on composedate(aDate)
    set y to (year of aDate as integer) as string
    set m to (month of aDate as integer) as string
    set d to (day of aDate as integer) as string
    if length of m < 2 then
        set m to "0" & m
    end if
    if length of d < 2 then
        set d to "0" & d
    end if
    set myDateString to y & m & d
    return myDateString
end composedate

1 个答案:

答案 0 :(得分:0)

创建系统事件是为了减轻Finder的大部分负担。 Finder很慢,因此尽可能使用系统事件。我不确定它将如何在64000文件的文件夹上执行,但试一试。

set myFolder to choose folder

tell application "System Events"
    set f to files of myFolder whose name extension is "jpg"
end tell

如果系统事件仍然太慢,那么单独使用applescript并不适合这么大的文件夹。即使你说你不想要,你也必须使用命令行。这样的事情应该更快......

set myFolder to (choose folder) as text
set jpgFiles to paragraphs of (do shell script "ls " & quoted form of POSIX path of myFolder & " | grep 'jpg$'")

repeat with aFile in jpgFiles
    set filePath to myFolder & aFile
    tell application "System Events"
        set theFileDate to creation date of file filePath
        -- do more stuff
    end tell
end repeat