如何移动文件夹中的每四个项目?

时间:2010-07-31 23:08:02

标签: macos applescript finder

我正在尝试创建一个脚本,将文件夹中的每四个项目移动到目标文件夹。到目前为止,我有:

tell application "Finder"
    set originFolder to folder choose folder
    set dest1Folder to folder choose folder
    set dest2Folder to folder choose folder
    set quarter to 1
    repeat with i from 1 to count of items in originFolder
        if quarter is 1 then
            move item i of originFolder to dest1Folder
        else
            move item i of originFolder to dest2Folder

        end if
        if quarter is 4 then
            set quarter to 0
        end if
        set quarter to quarter + 1
    end repeat
end tell

但是这给了我

error "Finder got an error: Expected a reference." number -1727

为什么这不起作用,什么会起作用?

1 个答案:

答案 0 :(得分:3)

您的脚本中存在一些问题:

设置choose folder个变量后,您必须在choose folder周围加上括号,以强制Finder将choose folder返回的alias转换为object specifier }}:

set originFolder to folder (choose folder)

移动项目的循环将以索引越界错误终止,因为originFolder中的项目数在每次迭代循环时都会发生变化,因为正在移动项目。

更好的解决方案是首先计算应移动的文件的索引,然后让Finder一步执行移动操作:

tell application "Finder"
    set originFolder to folder (choose folder)
    set destFolder to folder (choose folder)
    set indexes to {}
    repeat with i from 4 to (count of items in originFolder) by 4
        copy i to end of indexes
    end repeat
    move (every item of originFolder whose index is in indexes) to destFolder
end tell