How to open macOS FileProvider CloudStorage folder programmatically

I have an app with two targets:

  1. MainApp
  2. FileProvider Extension

I would like to be able to open the FileProvider CloudStorage folder in Finder from within the MainApp.

So far, I've only be able to achieve that by asking user to select his Home folder via NSOpenPanel, and then persisting security-scoped bookmark data, create symbolicLink in user's home folder and then later in the app:

let aliasLocationURL = try URL(resolvingBookmarkData: aliasLocationBookmarkData,
                            options: .withSecurityScope,
                            relativeTo: nil, bookmarkDataIsStale: &isStale)

aliasLocationURL.startAccessingSecurityScopedResource()
NSWorkspace.shared.open(aliasURL)
aliasLocationURL.stopAccessingSecurityScopedResource()

The problem is that if user does not select his Home folder via NSOpenPanel then my app gets an error message:

The application “MyApp” does not have permission to open “MyApp-FileProvider.”

I can get the path to CloudStorage folder via:

let fileProviderFolderURL = try await NSFileProviderManager(for: domain)?.getUserVisibleURL(for: .rootContainer)

and it properly states that fileProviderFolderURL is

/Users/me/Library/CloudStorage/MyApp-FileProvider

but whenever I try to open that path, it results in error.

Accepted Reply

Quite simple actually:

let domain = SREG.context.domain
guard let fileProviderFolderURL = try! await NSFileProviderManager(for: domain)?.getUserVisibleURL(for: .rootContainer) else {
    print("No CloudStorage URL found")
    return
}
let securityScopeResult = fileProviderFolderURL.startAccessingSecurityScopedResource()
if !securityScopeResult {
    L.error("Alias location URL security scope was not granted")
    showFinderInstructions = true
    return
}

let openResult = NSWorkspace.shared.open(fileProviderFolderURL)
if !openResult {
    L.error("NSWorkspace failed to open file provider folder.")
    showFinderInstructions = true
}
fileProviderFolderURL.stopAccessingSecurityScopedResource()

Replies

Quite simple actually:

let domain = SREG.context.domain
guard let fileProviderFolderURL = try! await NSFileProviderManager(for: domain)?.getUserVisibleURL(for: .rootContainer) else {
    print("No CloudStorage URL found")
    return
}
let securityScopeResult = fileProviderFolderURL.startAccessingSecurityScopedResource()
if !securityScopeResult {
    L.error("Alias location URL security scope was not granted")
    showFinderInstructions = true
    return
}

let openResult = NSWorkspace.shared.open(fileProviderFolderURL)
if !openResult {
    L.error("NSWorkspace failed to open file provider folder.")
    showFinderInstructions = true
}
fileProviderFolderURL.stopAccessingSecurityScopedResource()