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

Copy folder at current path with user generated name (applescript)

我正在尝试设置一个 applescript

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

这是我目前所拥有的,但我收到了错误:"File /Users/*****/Desktop/Temp/_scripts/ wasn’t found"

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 位于同一文件夹中。它需要的是文件而不是文件夹吗?

最严重的错误是 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

已经解释了为什么 OP 的代码没有按预期工作并且已经给出了可行的解决方案。

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

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/文件夹是否存在,如果不可控则创建的文件夹不存在