6

I have a simple python script which is moving files from my download folder. The script works fine when I running it via terminal. The issue happens when it gets run through launchd:

Traceback (most recent call last):
  File "/Users/ben/Project/Automation/CleanDownload.py", line 11, in <module>
    for f in listdir(downloadFolder):
OSError: [Errno 1] Operation not permitted: '/Users/ben/Downloads/'

Any Idea why?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
xpollcon
  • 525
  • 1
  • 6
  • 9

4 Answers4

4

Here is the workaround that I used to circumvent this issue. I was trying to run a simple shell script, but the same workaround would apply to a Python script. To summarize, the steps involved are:

  • Create an automator application called e.g. run-script.app that has a single bash script which runs whatever file is passed to it
  • Either give the full automator application Full Disk Access via Security & Privacy or run it once manually and click Allow when macOS prompts for permissions
  • Call the automator application with whatever script you want to run

More details:

  • Whatever script you're wanting to run, make sure it's executable (using chmod +x) and that you have the right #! line at the top of the script (e.g. #!/bin/bash). In this example, I'll use a script at ~/scripts/organize-screenshots.sh that moves screenshots from my desktop to my Google Drive directory:
#!/bin/bash
user_dir="/Users/soxley"

find "$user_dir"/Desktop -name 'Screen Shot *.png' -exec mv {} "$user_dir"/Google\ Drive/pictures/screenshots/ \;
  • Next, create an Automator application:
    • Open Automator
    • Click New Document
    • Select Application
    • Click Choose
    • Select Utilities > Run Shell Script
    • Select Pass Input: as arguments
    • Enter /bin/bash -c "$1" as the body of the script (see screenshot below)
    • Click File > Save and save the application wherever you'd like (run-script.app in this example)
  • Next, run the application that was just created manually to make sure it has the permissions it needs (you could also grant Full Disk Access to the new application in Security & Privacy):
    • Open Terminal.app
    • Execute the command open -a run-script.app organize-screenshots.sh
    • Click Allow when macOS asks if the application can access your Desktop
  • Now you're ready to configure your script in launchd. Update your .plist with the following ProgramArguments:
<key>ProgramArguments</key>
<array>
  <string>open</string>
  <string>-a</string>
  <string>/Users/soxley/scripts/run-script.app</string>
  <string>/Users/soxley/scripts/organize-screenshots.sh</string>
</array>

Now you should be able to run whatever script you want using this application as a wrapper. Automator Application Selection Automator Application Content

Steven Oxley
  • 6,563
  • 6
  • 43
  • 55
  • 1
    I had to add --args to be able to pass the script name to the run-script.app. – Erik Gomersbach Feb 28 '20 at 19:19
  • Automator script works & has full access, but launchd still does not. Could you provide the rest of the setup or a link to it? (There are too many different instructions) -- edit: It does work! My error was not using the user Library path `~/Library/LaunchAgents/` but `/Library/LaunchAgents/`. Thank you! – BadAtLaTeX Oct 16 '21 at 21:18
  • I'm trying this wrapper-app approach, but to obtain Camera permission, rather than Full Disk Access. I can create the app, run it from a terminal to get the permission prompt, and everything works fine. However, when I run it via my LaunchDaemon, I get "kLSNoLaunchPermissionErr: User doesn't have permission to launch the app (managed networks)". Is there a step that I'm missing? – DNS Feb 26 '22 at 02:06
0

if you havent seen this check out "OSError: [Errno 1] Operation not permitted" when installing Scrapy in OSX 10.11 (El Capitan) (System Integrity Protection)

and this https://apple.stackexchange.com/questions/339862/ls-operation-not-permitted-mojave-security

according to Roger go to Go to System Preferences -> Security & Privacy

and give Full Disk Access to Terminal.

https://discussions.apple.com/thread/8637915

Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
  • thanks for the answer as mentioned, it works fine from the terminal. The issue shows up when I'm running it from launchd. – xpollcon Dec 08 '19 at 11:01
0

Have you tried giving /sbin/launchd Full Disk Access?

benwiggy
  • 1,440
  • 17
  • 35
0

I was breaking my head on this issue for Big Sur for a LONG Time. What worked for me was the following :

  1. Grant Full Disk Access to Python3
  2. Grant Full Disk Access to launchd & launchctl