I'm trying to export a text document in the Documents directory and running into file permission problem in a Mac app that does not use the App Sandbox. As a workaround for the issue, I tried turning on the App Sandbox and giving read/write access to the Documents directory, but when I add the App Sandbox capability, I have the following entries for file access:
- User Selected File
- Downloads Folder
- Pictures Folder
- Music Folder
- Movies Folder
Since the file is a text file, exporting to any of the folders in the list makes no sense. If I give User Selected File read/write access, I still get file permission errors when I export. The UI in Xcode provides no way to add folders to the File Access list.
All I want to do is let someone export a file in the folder of their choice. How do I do this with the App Sandbox?
UPDATE
I'm developing a SwiftUI app. I was using SwiftUI's file exporter to export the document. I took robnotyou's suggestion to use NSSavePanel, but I still get the file permission error, with or without the App Sandbox. I get the following message in Xcode's console:
Error: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file
If I turn on the App Sandbox, give the Downloads folder read/write permission, and export the document there, the file permissions issue goes away.
I'm running macOS 11.5.2. If I open the Privacy preferences in System Preferences and select Files and Folders, I can see that some previous apps I developed appear in the list and have a checkbox selected that grants the app access to the Documents folder. The list of apps has an Add button, but it's disabled.
I tried giving this app full disk access and adding it to the Developer Tools list of apps that can run software locally that does not meet the system's security policy. But the file permission error persists.
I'm exporting the file in a folder inside the Documents directory. The file has read/write access for my user account and my group. The Documents folder has custom access when I get info on the folder in the file.
How do I get around this file permission error?
Does this mean my creating the temporary file wrapper is forbidden in the App Sandbox?
No, just that you have to use the right API to find the correct place to put your temporary. Specifically, see the discussion of .itemReplacementDirectory
in the url(for:in:appropriateFor:create:)
docs.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"