Search code examples
swiftcocoasandboxnsfilemanagerfindersync

FinderSync Extension runtime error: The file couldn’t be opened because you don’t have permission to view it


I'm writing a FinderSync extension that places an item in the context menu. When I right-click, I want the title of the menu item to change based on the selected file's contents. However, when I try to read the contents using Data(contentsOf: selectedUrl), it throws the following error:

Error Domain=NSCocoaErrorDomain Code=257 "The file “Some File.png” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/Shared/MySyncExtension Documents/Some File.png, NSUnderlyingError=0x61800004cf00 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

This does not change if I add or remove sandboxing.


Prior Research


Solution

  • The issue is that the Sandbox is preventing file access to the file, and thus your app cannot open it.

    Read and Write access for FinderSync extension in a sandboxed environment

    Even though the Finder Sync App Extension is granted "User Selected File" Sandbox File Access, the files accessed by the user via right-click seemingly do not count as being "user-selected".

    It may be possible to guide the user to select a root directory via an NSOpenPanel via Powerbox, after which all subdirectories can be accessed via a security-scoped bookmark.

    Using a Temporary Exception entitlement is also a possibility, but will cause App Review problems:

    Is there any way to give my sandboxed Mac app read only access to files in ~/Library?

    Please duplicate my Apple bug report requesting this behavior be allowed:

    Finder Sync App Extension selectedItemURLs() should receive "User Selected File" Sandbox file access.

    rdar://42874694
    https://openradar.appspot.com/radar?id=5063363058991104