It's either from a URL that stems from an NSOpenPanel or the result of a drag and drop operation.
var bookmarkData: Data?
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.canChooseDirectories = true
if openPanel.runModal() == NSApplication.ModalResponse.OK {
guard let url = openPanel.urls.first else {
return
}
do {
bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
} catch {
NSApplication.shared.presentError(error)
}
}
Trying to resolve the successfully obtained bookmark via the aforementioned URL initializer fails:
var isStale = false
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
It's also possible to reproduce this issue with an example project I've created for a previously reported sandbox-related issue:
https://github.com/fheidenreich/sandbox-fat32-rename
Choose, e.g., /System/Volumes/Data/Test/ via the "Select..." button.
Press "Create test file" to see the error occur.
As some background: I've not randomly tried to break things, but got a report from a user of my app describing this issue. Apparently, migration assistant created a new folder /System/Volumes/Data/Data/ when moving to Monterey where all the files reside which he tries to edit using my app.
It's of course possible to move the data to the User's folder, but I wanted to at least gather some understanding of the underlying issue and possibly prevent this from happening again.
Thanks for your reply!