Applescript 创建具有属性的文件夹

Applescript create folder with properties

这是我的代码:

on startButton_(sender)
    set folderDestination to choose folder
    log folderDestination
    log nameOfFolder
    tell application "Finder"
    make new folder at folderDestination with properties {name:nameOfFolder}
    end tell
end startButton_

想法是在目标文件夹 (folderDestination) 上创建一个文件夹,并使用文件夹的名称 (nameOfFolder)。 这是来自控制台的错误

2021-03-13 09:28:02.844240+0100 Folder Creator[1571:39314] *** -[AppDelegate startButton:]: Finder got an error: Can’t make «class ocid» id «data optr0000000059FEE7F626C77C59» into type Unicode text. (error -1700)

但是来自文件夹 Destination 和 name 的日志确实有效:

2021-03-13 09:28:02.750713+0100 Media Folder Creator[1571:39314] file:///Users/name/Desktop/

和:

2021-03-13 09:28:02.753207+0100 Media Folder Creator[1571:39314] folderName123

Xcode 中的文本字段连接到 nameOfFolder。 模型关键路径:nameOfFolder

不知道哪里出错了。 感谢您的帮助。

问题是 nameOfFolder 是一个 NSString 对象,您必须通过将其强制转换为 text:

来将其桥接到 AppleScript
make new folder at folderDestination with properties {name:nameOfFolder as text}

但是由于您已经处于 Cocoa 世界中,因此使用 NSFileManager 创建文件夹会更有效。

on startButton:sender
    set folderDestination to POSIX path of (choose folder)
    log folderDestination
    log nameOfFolder
    set fileManager to current application's NSFileManager's defaultManager()
    set {success, fmError} to fileManager's createDirectoryAtPath:(folderDestination & nameOfFolder as text) withIntermediateDirectories:false attributes:(missing value) |error|:(reference)
    if success is false then log fmError's localizedDescription()
end startButton: