【发布时间】:2014-04-13 23:41:16
【问题描述】:
我有一个 Python 脚本,它使用 subprocess 来运行改变我的桌面背景的 AppleScript。 launchd 会定期调用 bash 脚本来运行 Python 脚本。
当我从命令行运行 bash 脚本时,它工作正常,但问题是当 launchd 以完全相同的方式调用 bash 脚本时,AppleScript 似乎不起作用(我已经调试了它直到这个点,并且子流程调用之前的所有内容都可以正常工作)。我的猜测是它的权限问题,但 launchd 似乎正在像我一样运行脚本。我什至尝试过让它以 root 身份运行,但它仍然无法正常工作。作为守护进程和用户管理员 (/Library/LaunchAgents) 运行它也不起作用。
这是我的 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>Disabled</key>
<false/>
<key>Label</key>
<string>com.foo.background.change</string>
<key>Nice</key>
<integer>-15</integer>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>/Users/scott/Projects/Background/env/bin/Background/change_background.sh</string>
</array>
<key>ServiceDescription</key>
<string>Runs a script to change the desktop background image.</string>
<key>ServiceIPC</key>
<false/>
<key>StartInterval</key>
<integer>60</integer>
<key>UserName</key>
<string>scott</string>
</dict>
</plist>
感谢所有帮助!
编辑:
感谢您的回复!这是python脚本。 set_desktop_background 是从另一个确定使用哪个图像的方法调用的。始终使用完整的图像路径。
import subprocess
SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""
def set_desktop_background(filepath):
subprocess.Popen(SCRIPT%filepath, shell=True)
【问题讨论】:
-
你能给我们看一下 Python 脚本吗?或者至少部分使用
subprocess模块? -
听起来像是 PATH 问题。您是否在 subprocess 命令中指定了可执行文件的完整路径?
-
是的,请查看我的编辑。谢谢!