需要帮助使用 Launch Agent 在 mac 上启动 Python 脚本并将输出发送到窗口

发布于 2024-11-03 20:16:03 字数 1173 浏览 0 评论 0原文

我创建了一个 python 脚本,希望在登录 Mac 时自动运行,并且需要一些使用启动代理的帮助来执行此操作。到目前为止,我广泛使用了一切,所以我认为这是提问的地方,但我对此仍然相当陌生,所以我需要一些帮助。

这是我到目前为止所得到的以及我仍然需要的:

我有一个可以从终端运行的 python 脚本。每当它执行重要操作时(使用 python 'print' 命令),它都会定期向终端窗口输出文本。它还会在其他地方创建此日志,但我也希望在保持打开的窗口中进行实时更新。

我现在需要的是一个启动代理来在登录时自动启动此脚本并仍然输出此文本。我已经有了一个基本的启动代理,但它在后台运行脚本,我再也看不到输出了。根据我迄今为止的研究,看起来我可以使用 Lauch Agent .plist 中的“StandardOutPath”关键字来重定向此输出?我还不太了解这些程序中的 stdout/stdin/stderr ,无法理解所有这些是如何工作的,但我希望本质上重定向这个输出,也许到 xterm 窗口或类似的窗口。

我在任何地方都找不到此类事情的好例子,所以可能有更好的方法来做到这一点,而我只是没有寻找正确的事情。任何有关这一切如何运作的帮助或解释将不胜感激。

更新: 我的工作启动代理.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.name.WVCS-0.1</string>
    <key>RunAtLoad</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Path/to/StartupScript</string>
    </array>
</dict>
</plist>

I've created a python script I wish to have run automatically when I log in on a mac, and need some help using a Launch Agent to do this. I used SO extensively getting things working up to this point, so I figured this was the place to ask, but I'm still fairly new to this, so I need some help.

Here's what I've got so far and what I still need:

I have a python script that I can run from the terminal. It periodically will output text to the terminal window whenever it does something important (uses the python 'print' command). It also creates a log of this elsewhere, but I also want the realtime update in a window that stays open.

What I now need is a Launch Agent to start this script automatically on login and still output this text. I've gotten a basic Launch Agent working, but it runs the script in the background and I no longer can see the output. Based on my research so far, it looks like I can use the "StandardOutPath" keyword in the Lauch Agent .plist to redirect this output? I don't yet understand stdout/stdin/stderr in these programs well enough to understand how all of this works, but I was hoping to essentially redirect this output, perhaps to an xterm window or similar.

I couldn't find a good example of this type of thing anywhere, so there may be a better way to do it, and I'm just not searching for the right thing. Any help or explanation on how all this works would be much appreciated.

Updated:
My working Launch Agent .plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.name.WVCS-0.1</string>
    <key>RunAtLoad</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Path/to/StartupScript</string>
    </array>
</dict>
</plist>

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

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

发布评论

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

评论(1

∝单色的世界 2024-11-10 20:16:03

如果您希望输出出现在终端中,您可以使用 Launch Agent 启动 Apple 脚本,该脚本将打开终端,然后运行您的 python 脚本:

tell application "Terminal"
    activate
    do script "/usr/bin/python WVCS-0.1.py"
end tell

或者在 applescript / 调用窗口中显示文本

tell application "Terminal"
    activate
    do shell script "/usr/bin/python WVCS-0.1.py"
end tell

,然后更改您的 plist,以便

<array>
    <string>osascript</string>
    <string>/path/to/that/script</string>
</array>

不是超级优雅,但我希望有帮助!

If you want the output to appear in a terminal, you can use Launch Agent to start an Apple Script which will open Terminal and then run your python script:

tell application "Terminal"
    activate
    do script "/usr/bin/python WVCS-0.1.py"
end tell

or to display the text in the applescript / calling window

tell application "Terminal"
    activate
    do shell script "/usr/bin/python WVCS-0.1.py"
end tell

And then change your plist so that

<array>
    <string>osascript</string>
    <string>/path/to/that/script</string>
</array>

Not super elegant, but I hope that helps!

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