Is there a way to programmatically open a file from FileProvider Extension?
My use case is that I have FileProviderUI-extension for macOS which shows a dialog with 'Open file' button. And I'd like when user clicks on 'Open file' button to open the relevant file.
However, when I execute:
NSWorkspace.shared.open(itemURL)
a Finder error is shown: The application “MyAppAction (Finder)” does not have permission to open “My File.txt.”
I guess this is related to FileProviderExtension being sandboxed, but is there a way to open FileProvider files programmatically?
The solution is quite simple:
Task{
guard let fileProviderFolderURL = try! await NSFileProviderManager(for: domain)?.getUserVisibleURL(for: .rootContainer) else{
print("No CloudStorage URL found")
return
}
fileProviderFolderURL.startAccessingSecurityScopedResource()
let openResult = NSWorkspace.shared.open(fileProviderFolderURL)
if !openResult {
print("There was an error opening FileProvider Folder")
}
fileProviderFolderURL.stopAccessingSecurityScopedResource()
}
Just fetch the FileProvider item with getUserVisibleURL
and make sure startAccessingSecurityScopedResource
is invoked before accessing it.