将文件复制到另一个文件夹时如何修复 applescript 中的 ~10006 错误?

How to fix a ~10006 error in applescript when duplicating a file to another folder?

尝试将文件复制到另一个文件夹时,脚本会引发 ~10006 错误。这只发生在一些 Mac 迷你电脑上,在其他电脑上工作正常。我不知道为什么它在某些计算机上可以工作,但在其他计算机上却不能。

这是显示的错误: 无法将 "Macintosh HD:Users:username:Documents:" 设置为应用程序 "Finder" 的 <> "Macintosh HD:Users:username:Downloads:new test:portal resources"。 (~10006)

tell application "Finder"
set folderToBeMoved to (container of (path to me) as text) & 
"portal_resources"
set destinationFolder to path to documents folder as text
set moveFolder to duplicate folder folderToBeMoved to destinationFolder     with replacing
end tell

预期输出是将文件复制到文档文件夹。但是,在对某些 Mac 进行测试时,脚本会显示错误 ~10006。它在其他 Mac 上运行良好。

您要将文件夹复制到文字字符串(路径),这在机器 运行 旧系统版本上可能会失败。

删除 as text 参数以获得 alias 说明符

set destinationFolder to path to documents folder

试试这个代码:

tell application "Finder"
    set folderToBeMoved to folder "portal_resources" of container of (path to me)
    set destinationFolder to path to documents folder
    set moveFolder to duplicate folderToBeMoved to destinationFolder with replacing
end tell

这里有两个问题我已经更改了。首先,您将内容转换为文本字符串并尝试修改字符串,但 Finder 有丰富的语言来讨论文件对象。您应该将所有内容都保留为对象形式。例如,这个:

folder "portal_resources" of container of (path to me)

告诉查找器在那个容器中找到同名的文件夹和return一个你可以直接使用的对象说明符。

其次,一旦有了这个对象,就不能再给它添加 'folder' 说明符了。你说的地方:

duplicate folder folderToBeMoved

folderToBeMoved 已经是一个对象说明符('folder [path]' 形式的对象)所以你 实际上 向 Finder 询问 'folder folder [path],' 这会引发您看到的错误。这就像对某人说 "pass the 'pass the salt.'" 人们可能足够聪明,可以弄清楚; Finder 不是。