Page 1 of 3

another thorny permissions issue

Posted: Fri Jul 01, 2022 4:06 pm
by marksmithhfx
The final one before this app goes beta. I am looking for a better solution to this problem than the one I have created so far.

When my app starts it checks the documents folder for a file and adjusts some buttons in the app accordingly. macOS poses an alert saying "such-and-such program would like to access files in your Documents folder." with options "Don't Allow" and "Ok". Which is all fine, the user decides and we carry on (I have yet to test the "Don't Allow" option, I suspect my app is dead in the water at that point as it uses that location for a backup and some scratch files).

However later I want to access the Safari bookmarks file located in /Library/Safari/. The macOS does not present an alert allowing the user to authorise this, but instead just returns an empty zero-byte file to my app. I detect this and put up my own alert which at present just says

Code: Select all

"SafariSorter needs access to your bookmarks folder. Please enable Full Disk Access in System Preferences in the Privacy tab."
If the user then goes to the Full Disk Access privacy tab they will see my app listed there without a checkmark beside it and if they check it then the app will function properly. It reads the bookmarks file and allows you to sort the folders in various ways (Safari itself does not currently provide a mechanism for sorting folders, other than drag and drop, which can be quite tedious if you have a lot of folders -- I have a lot of folders. 190 and counting!!).

My app does have an info section and I provide the steps required to do this yourself:

1. Open System Preferences
2. Select the Privacy tab in the Security & Privacy section
3. From the left hand menu select "Full Disk Access"
4. Check the box next to "SafariSorter"

But I am looking for a more elegant solution (maybe using a pList or entitlements setting)?

I should add that I know that Apple does not want to encourage people to play with the Safari bookmarks file, but they do not restrict access if the user provides authorisation. I am just looking for a cleaner way to allow a user to provide that permission.

Thanks for any ideas you might have. For a guy who has 190 bookmark folders and 10's of thousands of bookmarks, sorting this mess was greatly facilitated by having this app, so at the very least I have a personal solution. Although, it might be nice to share it with the world.

Mark

Re: another thorny permissions issue

Posted: Sat Jul 02, 2022 12:22 pm
by stam
I don't know, is it really 'thorny'?

i have a number of paid apps on mac where there is a requirement to to do just what you do - ie allow full disk access through security settings.
In fact many of these apps provide a graphic step-by-step guide at startup to ensure the less computer literate users do this properly.

I suspect may mac users will to some extent be familiar with this...


S.

Re: another thorny permissions issue

Posted: Sat Jul 02, 2022 7:00 pm
by marksmithhfx
stam wrote:
Sat Jul 02, 2022 12:22 pm
I don't know, is it really 'thorny'?
Hi Stam,

To the extent there does not appear to be an elegant way to facilitate the user providing access (as happens, for example, with the Documents folder access I presented earlier) then I do think it is problematic to deal with. Really this is down to the OS needing to ensure the user provides authorisation for the access (as they do, for example, with camera use).
stam wrote:
Sat Jul 02, 2022 12:22 pm
i have a number of paid apps on mac where there is a requirement to to do just what you do - ie allow full disk access through security settings.
In fact many of these apps provide a graphic step-by-step guide at startup to ensure the less computer literate users do this properly.
I would love to see some screen shots of this, if possible. I'll also send you a link so you can see my current approach and ask you to comment on possible improvements.

But while I am on the topic of "thorny" issues, do you know if it is possible to detect if an app is running (ie. open) on a Mac from another app? I am sure there is a way since sometimes when I run something (say, a disk scanner) it will ask me to close some app, for example. However, I'm not sure there is a way to do this from LC at present. Have you ever run across anything like that?

Thanks
Mark

Re: another thorny permissions issue

Posted: Sun Jul 03, 2022 2:29 pm
by paul@researchware.com
We ran into this as well. There is (or was as of Big Sur, I have not checked recently for any changes) no Entitlements you can add to you app's entitlement list to allow Full Disk Access. According to Apple, only users can grant this. And as you have already seen, the OS does not prompt for such access.

The closest we came up with was a URI to open the System Preferences and Privacy and Security tab for the user:

If you have a link or a button that executes:
launch url "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles//"

It will open the correct panel. The user still needs to scroll to the Full Disk Access, unlock the setting with their admin password, and manually add the app.

Hope this helps a little. If anyone does find some Entitlement for Full Disk Access, please post it!

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 9:46 am
by marksmithhfx
paul@researchware.com wrote:
Sun Jul 03, 2022 2:29 pm
The closest we came up with was a URI to open the System Preferences and Privacy and Security tab for the user:

If you have a link or a button that executes:
launch url "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles//"

It will open the correct panel. The user still needs to scroll to the Full Disk Access, unlock the setting with their admin password, and manually add the app.
Paul, thank you. That is incredibly useful information and does present the user with many fewer steps to have to wrangle with.

Much appreciated,
Mark

PS do you happen to know of a way to detect if a specific application, like say Safari, is "open"?

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 1:04 pm
by paul@researchware.com
marksmithhfx wrote:
Mon Jul 04, 2022 9:46 am
PS do you happen to know of a way to detect if a specific application, like say Safari, is "open"?
Not off the top of my head. If you launch url "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles//" no browser is opened, just the System Preferences window.

This article gives a few command line commands for macOS that return a list of open processes/apps. See https://macreports.com/how-to-tell-what ... -your-mac/ and scroll down.

I've not tried any of them, but I expect you could use shell() to get a list of open processes and then filter or search it for Safari.

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 3:34 pm
by stam
marksmithhfx wrote:
Mon Jul 04, 2022 9:46 am
PS do you happen to know of a way to detect if a specific application, like say Safari, is "open"?
AppleScript will help you do this (since your target OS is MacOS) - see the discussion on StackOverflow: https://stackoverflow.com/questions/142 ... pplescript

HTH
Stam

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 5:47 pm
by FourthWorld
shell("top") will return a list of running processes.

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 9:37 pm
by marksmithhfx
paul@researchware.com wrote:
Mon Jul 04, 2022 1:04 pm
This article gives a few command line commands for macOS that return a list of open processes/apps. See https://macreports.com/how-to-tell-what ... -your-mac/ and scroll down.

I've not tried any of them, but I expect you could use shell() to get a list of open processes and then filter or search it for Safari.
Thanks Paul. The command

Code: Select all

top -o rsize
seems to look most promising because if Safari is "launched" it is using up some memory even if it is not currently using any cpu... so it reliably shows up in that list.

I tried

Code: Select all

put shell("top -o rsize") into myVar
thinking I might parse it after, but it hung LC. Any alternatives you can think of?

Mark

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 9:51 pm
by marksmithhfx
BTW, I also tried

Code: Select all

get shell("top -o rsize")
and had no better luck.

Mark

Re: another thorny permissions issue

Posted: Mon Jul 04, 2022 10:00 pm
by marksmithhfx
FourthWorld wrote:
Mon Jul 04, 2022 5:47 pm
shell("top") will return a list of running processes.
Thanks Richard. LC 9.6.8 rc2 using

Code: Select all

get shell("top")
assuming it goes into the it variable also hangs livecode. Is there an alternative I'm not thinking of?

Thanks
Mark

Re: another thorny permissions issue

Posted: Tue Jul 05, 2022 12:41 pm
by marksmithhfx
A correction. The above unix commands do not crash LC but they do block LC because they are non-terminating. I did manage to redirect the output at the terminal level to a text file and it just filled that to about 5 MB's in a few seconds. But, after a short course in Unix commands I did manage to hobble this together:

Code: Select all

   put shell("top -o rsize -ncols 2 -l 1") into <container>
This produces a 2 col output after "1" iteration that is about 16k in size, and yes, Safari showed up, so mission accomplished.

All 11 columns are listed below (but not all rows), but -ncols can be used to trim this to just 2. BTW, if all columns are captured the resulting output is about 190k in size so trimming is worthwhile if you are directing this to a variable.

Code: Select all

Processes: 611 total, 2 running, 609 sleeping, 1997 threads            21:30:21
Load Avg: 1.30, 1.46, 1.55  CPU usage: 1.86% user, 3.3% sys, 95.10% idle
SharedLibs: 403M resident, 86M data, 20M linkedit.
MemRegions: 101621 total, 2143M resident, 133M private, 771M shared.
PhysMem: 8074M used (1768M wired), 117M unused.
VM: 20T vsize, 3141M framework vsize, 2183785(0) swapins, 2950234(0) swapouts.
Networks: packets: 19628485/23G in, 3987548/1157M out.
Disks: 11116441/142G read, 4665439/77G written.

PID    COMMAND      %CPU TIME     #TH   #WQ  #PORT MEM    PURG   CMPRS  PGRP
158    WindowServer 4.3  08:10:53 12    5    4951+ 880M+  16M+   243M   158
0      kernel_task  2.0  03:58:52 176/4 0    0     488M   0B     0B     0
601    Dropbox      0.7  39:11.14 140   1    2071  456M   0B     195M   601
6882   Safari       0.0  53:54.84 8     2    3594+ 413M+  0B     291M   6882
429    Finder       0.0  23:38.42 5     2    7311  293M   80K    204M   429
37394  com.apple.We 0.0  00:14.99 7     3/2  96    244M   0B     16M    37394
403    Mail         0.0  31:04.79 8     1    1975- 228M-  1024K  102M   403
37555  LiveCode     2.5  00:32.78 11    4    639   215M   18M    0B     37555
34338  Microsoft Wo 0.0  00:23.48 15    5    376-  169M-  64K    148M   34338
Thanks to all for the suggestions, it's been very useful. However this approach does take some time to execute... in my experience about .5 seconds on a MacBook Pro so I'll be on the hunt for something that takes a millisecond or two if possible.

But this is a solution so thank you, thank you, thank you.

Mark

Re: another thorny permissions issue

Posted: Tue Jul 05, 2022 1:01 pm
by richmond62
I thought I would be clever and try this:

Code: Select all

on mouseUp
   if (shell("top -o rsize -ncols 2 -l 1")) contains "safari" then
      put "Yes"
   else
      put "No"
      end if
end mouseUp
Now, Safari was NOT running but I still got "Yes" because

SafariBookmarksSyncAgent
com.apple.Safari.SafeBrowsing.Service and
SafariLaunchAgent

were running [which is a load of cack as I NEVER use Safari - so those things are merrily slurping RAM for no good reason].

HOWEVER:

Code: Select all

on mouseUp
   if (shell("top -o rsize -ncols 2 -l 1")) contains "safari " then
      put "Yes"
   else
      put "No"
      end if
end mouseUp
the addition of a SPACE makes ALL the difference. :)

Just checked WITH & WITHOUT Safari running.

Re: another thorny permissions issue

Posted: Tue Jul 05, 2022 1:14 pm
by richmond62
Fekking Apple:

am 'off' to try and prevent

SafariBookmarksSyncAgent
com.apple.Safari.SafeBrowsing.Service and
SafariLaunchAgent

running in the background.

AND 50,000 DropBox Finder Extensions!

Re: another thorny permissions issue

Posted: Tue Jul 05, 2022 1:20 pm
by marksmithhfx
richmond62 wrote:
Tue Jul 05, 2022 1:01 pm
I thought I would be clever and try this:

Code: Select all

on mouseUp
   if (shell("top -o rsize -ncols 2 -l 1")) contains "safari" then
      put "Yes"
   else
      put "No"
      end if
end mouseUp
Richmond, well done!! That was my next task; to try and wrangle the output into something useful and here you've done it for me on a single line. Sheesh, the things I have yet to learn about LC. Anyway, I shall be stealing your solution, including the spaces trick 😊

Mark