简体   繁体   English

在 Mac OSX 中运行 python 脚本启动权限问题

[英]Running python script in Mac OSX launchd permission issue

I have a simple python script which is moving files from my download folder.我有一个简单的 python 脚本,它正在从我的下载文件夹中移动文件。 The script works fine when I running it via terminal.当我通过终端运行该脚本时,它运行良好。 The issue happens when it gets run through launchd:当它通过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?任何想法为什么?

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.我试图运行一个简单的 shell 脚本,但同样的解决方法也适用于 Python 脚本。 To summarize, the steps involved are:总结一下,涉及的步骤是:

  • Create an automator application called eg run-script.app that has a single bash script which runs whatever file is passed to it创建一个名为例如 run-script.app 的自动化应用程序,它有一个 bash 脚本,该脚本运行传递给它的任何文件
  • 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要么通过安全性和隐私为全自动应用程序提供全磁盘访问权限,要么手动运行一次,然后在 macOS 提示权限时单击允许
  • 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 #!无论您想运行什么脚本,请确保它是可执行的(使用chmod +x )并且您拥有正确的#! line at the top of the script (eg #!/bin/bash ).脚本顶部的行(例如#!/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:在此示例中,我将使用~/scripts/organize-screenshots.sh中的~/scripts/organize-screenshots.sh将屏幕截图从我的桌面移动到我的 Google Drive 目录:
#!/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:接下来,创建一个 Automator 应用程序:
    • Open Automator打开自动机
    • Click New Document单击新建文档
    • Select Application选择应用
    • Click Choose点击选择
    • Select Utilities > Run Shell Script选择实用程序 > 运行 Shell 脚本
    • Select Pass Input: as arguments选择传递输入:作为参数
    • Enter /bin/bash -c "$1" as the body of the script (see screenshot below)输入/bin/bash -c "$1"作为脚本的主体(见下面的截图)
    • Click File > Save and save the application wherever you'd like ( run-script.app in this example)单击“文件”>“保存”并将应用程序保存到您想要的任何位置(在本例中为run-script.app
  • 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 ):接下来,运行刚刚手动创建的应用程序以确保它具有所需的权限(您还可以在Security & Privacy 中授予新应用程序的完整磁盘访问权限):
    • Open Terminal.app打开Terminal.app
    • Execute the command open -a run-script.app organize-screenshots.sh执行命令open -a run-script.app organize-screenshots.sh
    • Click Allow when macOS asks if the application can access your Desktop当 macOS 询问应用程序是否可以访问您的桌面时,单击允许
  • Now you're ready to configure your script in launchd.现在您已准备好在 launchd 中配置您的脚本。 Update your .plist with the following ProgramArguments :使用以下ProgramArguments更新您的 .plist :
<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.现在您应该能够使用此应用程序作为包装器运行您想要的任何脚本。 自动化应用选择 自动化应用内容

您是否尝试过授予/sbin/launchd全盘访问权限?

I was breaking my head on this issue for Big Sur for a LONG Time.很长一段时间以来,我一直在为 Big Sur 打破这个问题。 What worked for me was the following:对我有用的是以下内容:

  1. Grant Full Disk Access to Python3授予对 Python3 的全盘访问权限
  2. Grant Full Disk Access to launchd & launchctl授予对 launchd 和 launchctl 的全盘访问权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM