How do I find out if the user has denied folder access?

I want to read the desktop folder of MacOS. For this I determine the number of items with:

i = SpecialFolder.Desktop.Count

How do I find out if the user has denied access?

image

No IOException is thrown.

Thank you for any tips.

If it returns 0, then it is denied?
There will always be at least 1 item, won’t there?

I have to assume that the folder can be empty and also return zero.

The desktop?

Make a folderitem for the desktop, as follows

fptr = SpecialFolder.Desktop
if  (fptr.Exists=False or fptr.IsReadable=False or fptr.IsWriteable=False)  then
  // handle user's refusal here
end if

This is what I do in my app (for the Documents folder rather tha Desktop) and it seems to do the job.

I found that LastErrorCode returns the value 257 when access was denied.

But FolderItem.LastErrorCode is marked as deprecated!

if (fptr.Exists=False or fptr.IsReadable=False or fptr.IsWriteable=False)

This works perfectly!
Thanks for the solution.

The only file in the desktop folder that is expected to be always there is the “.localized” file (which tells Mac OS that the displayed name can be localized). That said, the user may very well delete this file, rendering the desktop folder potentially empty.

No .DS_Store file there ? (since the folder is always open, I would expect one, but I am not sure and cannot verify: I need a newly opened mac computer to test it).

.DS_Store files are created by the Finder. I use Path Finder instead, so this file isn’t guaranteed to be there.

The “always open” folder you’re referring to is the real desktop (where the wallpaper is shown), correct?
As far as I know, .DS_Store files are for storing settings of a folder viewed in a window. While I may be wrong about that fact, this would mean the .DS_Store file is not always in the Desktop folder. :man_shrugging:

To read it?

Handling “Can’t read” should be enough and appropriated, unless you intend writing something there

If  Not SpecialFolder.Desktop.IsReadable  Then
  // handle user's refusal here
End

Yes the .DS_store file for the Desktop could be deleted. But if the Desktop is otherwise empty, the .DS_Store file is “empty”, too.

In my case, I was expecting to read and write in the Documents folder.

My “if” statement tests three conditions. It’s possible that exactly which fails when the user refuses depends on which macOS version is running.

Also, write-only folders are supported on Mac OS. You can’t read them, but you can write to them (e.g. the ~/public/Drop Box folder (not to be confused with the dropbox software)).

You don’t need to check f.exists, not exists implies not read and not write.

In most modern OSs, that’s why I said

In a write-only folder you probably couldn’t list the files and count them if the OS denies file traversal when not readable. I guess the OS should forbid you to change your desktop to a write only because it probably will cause lots of headaches, maybe crashes.

No. This is not how .DS_Store works. It also holds the folder location/width/height. So, it is created at Open time or Close time.

But the Destop Folder is both the screen background and a Folder.
This is why I think there cn be a debate unless one test a freshly opened mac computer and check (not using the Finder; Xojo and AppleScript can be used for examples) the situation.