如何创建一个 Automator 应用程序,该应用程序获取一个文件作为参数,并在 macOS Monterey 中使用该文件运行 python 脚本?

发布于 2025-01-16 05:20:51 字数 145 浏览 3 评论 0原文

如何使用 automator 创建一个 mac 应用程序,获取一个我“放入”其中的文件并使用该文件作为参数执行 python 脚本?

我尝试了建议在 Automator 中创建“服务”的各种问题的解决方案,但我在蒙特雷的 Automator 中没有看到这样的选项

How do I create a mac application using automator, that gets a file that I "drop" in it and executes a python script with this file as an argument?

I tried solutions from various questions that suggest creating a "service" in Automator, but I don't see such option in Automator in Monterey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2025-01-23 05:20:51
  1. 在 Automator 中创建一个新的应用程序
  2. 从左侧边栏中选择“运行 shell 脚本”操作,然后双击它来添加它
  3. 将 shell 更改为 /bin/bash/
  4. 更改 < em>将输入传递给作为参数(请参阅下面的屏幕截图)

<图片src="https://i.sstatic.net/PpW4h.jpg" alt="创建的自动化的屏幕截图">

  1. 添加代码到“运行 shell 脚本块”,例如添加以下代码以将目录更改为您的 python 脚本目录,激活虚拟环境并使用作为参数传递的文件从中运行脚本(使用 "$1" 获取文件作为参数):
cd my_project_directory
source .venv/bin/activate
python my_script.py "$1"
  1. 将自动化保存为应用程序
  2. 您现在可以拖动并将任何文件拖放到 Finder 中保存的应用程序的应用程序图标中将其传递给脚本,它将执行

编辑:

如果您希望能够一次将多个文件拖放到应用程序上并按顺序为每个文件运行脚本,则可以将最后一行更改为以下代码:

for f in "$@"
do
    python my_script.py "$f"
done
  1. Create a new Application in Automator
  2. Select "run shell script" action from the left sidebar and add it by double-clicking on it
  3. Change shell to /bin/bash/
  4. Change Pass input to as arguments (refer to a screenshot below)

screenshot of a created automation

  1. Add code to "run shell script block", e.g. add the following code to change directory to your python script directory, activate virtual environment and run a script from it with file passed as argument (use "$1" to get a file as argument):
cd my_project_directory
source .venv/bin/activate
python my_script.py "$1"
  1. Save your automation as an application
  2. You can now drag and drop any file to the application icon of your saved app in finder to pass it to the script and it will execute

EDIT:

If you want to be able to drop several files on your app at once and run a script for each of them in a sequence, you can change the last line to the following code:

for f in "$@"
do
    python my_script.py "$f"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文