首页 > 解决方案 > Applescript 创建具有属性的文件夹

问题描述

这是我的代码:

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

我不知道错误在哪里。谢谢你的帮助。

标签: applescript

解决方案


问题是nameOfFolder一个NSString对象,您必须通过强制将其桥接到 AppleScript text

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:

推荐阅读