Folder Count is 0 outside ApplicationData on macOS

When I set a FolderItem to a folder within my ApplicationData folder, f.Count is correct and > 0. When I run the same code on a folder outside my ApplicationData folder (eg Documents), it always returns zero (I have the same results with FileListMBS.Count).

I assumed this was an AppWrapper capabilities issue, so I gave myself access to the Documents folder, but the result is still zero.

How can I get the f.Count outside of the ApplicationData folder in a signed app?

Which version of macOS and which version of Xojo do you use?

On Crapolina and higher you should see the wonderful “do you really want to” dialogs before you have access to a folder. The user needs to select the folder.

Xojo 2021R3.1 on macOS Monterey 12.2 beta 2.

I thought that if I gave my App rights to the Documents folder, then I could scan the folder for files. Is this wrong?

No, that works fine. So you must be doing something different. Can you show your code and describe what happens?

I have a list of NativePath pathnames stored in a database. As an example NativePathToFind is a folder (/Users/davidcox/Documents/CubeiTz Decrypted/0000033439).

If I remove the AppWrapper Script I can access the f.Count in this Documents subfolder, so I assume it is a signing and permissions issue. My only question is how can I gain access to ‘see’ these files?!

Here is my code:

Var NativePathToFind As String = AllNativePaths(tempInt)
f = New FolderItem(NativePathToFind, FolderItem.PathModes.Native)
'f = SpecialFolder.ApplicationData 'this works OK
Var AllFiles() As FolderItem

If f <> Nil And f.Exists Then 'f.Count = 0 here
  If f.IsFolder Then
    Var tempFileListMBS As New FileListMBS(f) 'tempFileListMBS.Count = 0 here
    Var NumFiles As Integer = tempFileListMBS.Count - 1
    For tempInt2 As Integer = 0 To NumFiles 'this never loops as NumFiles = -1
      Var g As FolderItem = tempFileListMBS.TrueItem(tempInt2)
      If g <> Nil And g.Exists And Not g.IsFolder Then
      End If
  End If

Maybe more “added security” in macOS? I’ve saw some apps in trouble in Monterey needing:

Beginning with macOS Monterey, some apps requires permissions to touch some places Apple protects. To bypass it:

  1. Click the Apple menu at the top of the screen.
  2. Select System Preferences > Security & Privacy.
  3. Click the lock, enter your password for the device, and click Unlock.
  4. Select the Privacy tab, then select Full Disk Access.
  5. Select your app

No, that’s not a good idea. The setting is for backup apps or app like mine that need to access Mail data.

You must have the user select the folder first. Then you can iterate over the folder.

If you know the list of places that Apple is denying and the app needs, you are right. That recipe is just the easy “let’s try to make that trusted crashing app to work”.

This is Apple’s philosophy. There is no way around. I find those dialogs stupid. They just train users to click. It’s like clicker training for dogs.

Is your app sandboxed?

Yes, this seems to be the source of my problem. Unsandboxed it shows the files within a folder, but sandboxed with AppWrapper I can no longer scan for files.

I don’t have the luxury of getting users to select the folder (as I do elsewhere) as I am checking through a list of FolderItems previously chosen by the user (eg in another session). I am happy that it only checks within the Documents folder, but how do I get macOS/AppWrapper to allow me to scan for files within Documents?

For a Sandboxed app you need to use “security scoped bookmarks”. Check the forum for code for MBS and Sam from Ohanaware.

I have tried all day to get either SSFolderitem or CFBookmarkMBS to give me Documents folder access in a sandboxed application and have failed.

I can get a BookMark from a FolderItem and resolve a BookMark to a FolderItem, but have failed to have them give me permission to access the file/folder in a sandboxed application.

Did you try Christians example at /MacCF/Sandbox File Access/Sandbox Files ? Its about accessing a file, but can for sure be adopted to folders…

Yes, it’s his only example.

Where did you fail? The example is working fine, at least for me…