Move selected file

I have a Swift macOS application, and I'm trying to allow the user to open a file using NSOpenPanel and then rename that file to another name.

I have enabled sandboxing with UserSelectedFile set to Read/Write.

Here is my code:

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)
    }
}

It simply asks the user to choose a file, and then renames the file to "New name.test". However, I get an error message saying

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"}}

I'm pretty sure this is because even though I have access to that specific file, I do not have access to its parent directory which is what I need in order to rename the file.

This question is very similar to Application Sandbox: renaming a file doesn't work, however none of the answers work for me.

The most promising answer is here, and I tried adding the document types & using NSFileCoordinator, but it still didn't work.

Is this even possible, or am I missing something obvious?



from Recent Questions - Stack Overflow https://ift.tt/3kSQY90
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)