我有一个 Swift macOS 应用程序,我试图允许用户使用打开一个文件NSOpenPanel
,然后将该文件重命名为另一个名称。
我已启用沙盒并User Selected File
设置为读/写。
这是我的代码:
let panel = NSOpenPanel()
panel.canChooseFiles = true
panel.canChooseDirectories = false
panel.allowsMultipleSelection = false
if panel.runModal() == .OK {
let url = panel.url!
do {
try FileManager.default.moveItem(at: url, to: url.deletingLastPathComponent().appendingPathComponent("New name.test"))
}
catch let x {
print(x)
}
}
它只是要求用户选择一个文件,然后将文件重命名为"New name.test"
. 但是,我收到一条错误消息说
Error Domain=NSCocoaErrorDomain Code=513 "“Untitled” couldn’t be moved because you don’t have permission to access “untitled folder 30”." UserInfo={NSSourceFilePathErrorKey=/Users/user/Desktop/untitled folder 30/Untitled.txt, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/user/Desktop/untitled folder 30/New name.test, NSFilePath=/Users/user/Desktop/untitled folder 30/Untitled.txt, NSUnderlyingError=0x6000032115c0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
我很确定这是因为即使我可以访问该特定文件,我也无权访问它的父目录,这是我重命名文件所需要的。
这个问题与Application Sandbox: renameing a file doesn't work非常相似,但是没有一个答案对我有用。
最有希望的答案在这里,我尝试添加文档类型和使用NSFileCoordinator
,但它仍然没有工作,出现这个错误:
an error was received from pboxd instead of a token. Domain: NSPOSIXErrorDomain, code: 1
这甚至可能吗,还是我错过了一些明显的东西?