Unable to Create Files Adjacent to User-Selected File Due to App Sandbox Permissions

I am developing a macOS app that requires the ability to create new files in the same directory as a user-selected file, but I am encountering permission issues due to the App Sandbox restrictions. While the user can select a file (e.g., a.jpg) using a standard open panel, I cannot create an adjacent file (e.g., a.jxl) in the same folder because the sandbox only grants access to the selected file, not to other files in the directory.

I understand that full disk access might be an option, but it requires user intervention and isn't suitable for this case. Is there any way to extend access to other files in the directory (including those not selected by the user) while remaining within the App Sandbox environment?

Answered by DTS Engineer in 815242022

Take a look at document-relative security scoped bookmarks.

This might not be precisely what you want, but it's worth a look.

Take a look at document-relative security scoped bookmarks.

This might not be precisely what you want, but it's worth a look.

If I understand your question correctly, you can grant access to a directory as well.

What @DTS Engineer said, plus…

The way I do this is in 2 steps:

  • A first NSPanel asks for Directory to select
  • Then, in the openPanel.begin() closure I ask for saving the file
  • Now, you will be able to save in this Directory
        let openPanel = NSOpenPanel()       // to authorize access to directory
        openPanel.message = NSLocalizedString("Select Directory)", comment: "")  
        openPanel.prompt = NSLocalizedString("Select", comment: "")  
        openPanel.canChooseFiles = false   
        openPanel.canChooseDirectories = true   // To select directrory
        openPanel.canCreateDirectories = true

        openPanel.begin() {  (result2) -> Void in
            if result2 == NSApplication.ModalResponse.OK {
                // When obtaining access through NSOpenPanel, you don't need to call startAccessingSecurityScopedResource() and stopAccessingSecurityScopedResource(), because the user explicitly granted you access for this session.
                
                let folderUrl = openPanel.url! 

                // Create file
                let savePanel = NSSavePanel()
                savePanel.title = NSLocalizedString("File", comment: "")  
                savePanel.nameFieldStringValue = "" // nothing here, will be entered by user
                savePanel.prompt = NSLocalizedString("Create", comment: "")   
                savePanel.allowedFileTypes = ["XXXX"]  // your type
                let fileManager = FileManager.default   
                
                // Define new file name
                savePanel.begin() { (result) -> Void in
                    if result == NSApplication.ModalResponse.OK { 
                         // write the file
                         // save bookmarks
                    }
            }

Hope that helps.

Unable to Create Files Adjacent to User-Selected File Due to App Sandbox Permissions
 
 
Q