如何使用 AppleScript 迭代移动桌面上的每个文件夹?

How can I move each folder on my desktop iteratively with AppleScript?

我正在尝试编写一个 AppleScript,它遍历我的桌面文件夹中的文件夹并将它们移动到名为 "destination" 的子文件夹中。 (我需要遍历桌面文件夹而不是一次移动它们,因为我希望根据随机生成的数字移动或不移动每个文件夹)。

我的代码:

set desktopFolders to (path to desktop as text)
set destinationFolder to (path to desktop as text) & "destination"

    tell application "System Events"
        set subFolders to (get every disk item of folder desktopFolders)
        repeat with eachFolder in subFolders
            move eachFolder to destinationFolder
        end repeat
    end tell

如果我尝试将整个 subFolders 变量移动到 destinationFolder,该脚本会工作,并且它不会因尝试遍历 subFolders 中的 eachFolder 而引发错误=].但是,当我尝试将 eachFolder 移动到 destinationFolder 时,出现以下错误:

System Events got an error: NSArgumentsWrongScriptError

为什么 eachFolder 变量不能以这种方式移动?有没有更好的方法来遍历我的桌面文件夹中的文件夹,以便我可以单独移动它们中的每一个?

desktopFolders 更改为:desktopFolder

  • 注意末尾没有 s,因为 Desktop folder 是单数而不是复数。

更改 disk item 于:

set subFolders to (get every disk item of folder desktopFolder)

收件人:folder

另加:

whose name is not "destination"

结束于:

get every folder of folder desktopFolder

示例:

set subFolders to (get every folder of folder desktopFolder whose name is not "destination")

您不想尝试将 destination 文件夹 移动到它本身。

Sans 使 desktopFolders 成为单数,这些其他更改允许 代码 的其余部分在 macOS High Sierra[= 下测试时无错误地工作44=].