使用用户生成的名称(applescript)在当前路径下复制文件夹

时间:2019-02-11 14:35:06

标签: applescript

我正在尝试建立一个Applescript,

  1. 要求用户输入文本
  2. 使用输入数据创建一个新文件夹
  3. 将文件从另一个文件夹复制到此文件夹

这是我到目前为止的内容,但出现错误:“未找到文件/ Users / ***** / Desktop / Temp / _scripts /”

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me) as alias

    set newclient to make new folder at loc with properties {name:newfoldername}
    set structure to ((POSIX path of loc) & "_scripts/") as alias

    duplicate folder structure to loc

end tell

_scripts文件夹与我的applescript位于同一文件夹中。是要文件而不是文件夹吗?

2 个答案:

答案 0 :(得分:1)

最重要的错误是Finder无法识别POSIX路径。

如果要将与运行脚本相同级别的文件夹"_scripts"复制到新创建的文件夹,只需使用Finder说明符语法(folder "_scripts" of loc

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me)

    set newclient to make new folder at loc with properties {name:newfoldername}
    duplicate folder "_scripts" of loc to newclient

end tell

答案 1 :(得分:1)

Vadian's Answer解释了为什么OP的代码无法按预期运行并且已经给出了可行的解决方案。

对于其他可能需要别名来重用的其他人,我们也可以通过在路径字符串之前明确说出“ POSIX文件”来转换文件路径格式:

set structure to POSIX file ((POSIX path of loc) & "_scripts/") as alias

(由于脚本应该复制到newclient,所以最后一行也要修改,)完整代码如下:

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me) as alias

    set newclient to make new folder at loc with properties {name:newfoldername}
    set structure to POSIX file ((POSIX path of loc) & "_scripts/") as alias

    duplicate folder structure to newclient

end tell

P.S。还建议检查_scripts/文件夹是否存在以及如果无法控制将要创建的文件夹是否存在。

相关问题