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

发布于 2024-09-12 11:19:20 字数 744 浏览 7 评论 0原文

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

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

为什么这不起作用以及什么会起作用?

I'm trying to make a script that will move every fourth item in a folder to a destination folder. So far I have:

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

but this gives me

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

Why isn't this this working and what will work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-09-19 11:19:20

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

在设置 choosefolder 变量时,您必须在 choosefolder 两边加上括号,以强制 Finder 将 别名选择文件夹返回到对象说明符

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

There are a couple of problems in you script:

Upon setting up the choose folder variables you have to put parentheses around choose folder in order to force the Finder to convert the alias returned by choose folder to an object specifier:

set originFolder to folder (choose folder)

The loop that moves the items will terminate with an index out of bounds error because the number of items in the originFolder changes upon each iteration through the loop, as items are being moved.

A better solution is to first compute the indexes of the files that should be moved and then have the Finder perform the move operation in one step:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文