0

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?

mixtly87
  • 1,675
  • 15
  • 32

1 Answers1

0

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.

mixtly87
  • 1,675
  • 15
  • 32
  • But this seems to work only if user selected his Home folder for place where Bookmark Alias folder should be created. If it was created in ie Documents folder, then `NSWorkspace.shared.open(...)` will fail. – mixtly87 Apr 18 '23 at 18:16