由于某种原因,Dropbox在联机几天后终止(崩溃或退出(,没有任何解释。
因此,我开始研究AppleScript
在应用程序终止时自动重新启动应用程序的方法。
这让我想到了这个脚本:
repeat
delay 120 #Run every two minutes
tell application "System Events"
if name of every process does not contain "Dropbox" then tell application "Dropbox" to launch
end tell
delay 5
end repeat
我还希望脚本在后台运行,所以我为launchctl
实现了我自己的Ask Different解决方案变体。
在~/Library/LaunchAgents/
中,我创建了一个名为dropbox-keep-alive.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>dropbox-keep-alive.job</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/xxx/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/dropbox-keep-alive.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
AppleScript的路径在上面的<array>
中给出,launchutil
的.job
标签在<key>
下分配。
然后我加载.plist
:
launchctl load -w ~/Library/LaunchAgents/dropbox-keep-alive.plist
然后启动它:
launchctl start dropbox-keep-alive.job
为了测试,我退出了Dropbox,然后等待了2分钟以上,但什么也没发生。
如果我再次尝试launchctl load -w
,我会得到消息,它已经加载。launchctl start
没有给出响应消息。
我知道AppleScript之所以有效,是因为当它直接与osascript
一起运行时,它是有效的。但在.plist
的某个地方——或者我对launchctl
的管理——有一些东西不起作用。
我已经尝试过launchctl unload -w
脚本并重新进行该过程。有什么想法吗?
launchd不对字符串进行shell风格的解析,因此脚本路径中的转义将被解释为实际文件名的一部分。。。并且将找不到它。它应该看起来更像这样:
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/xxx/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/dropbox-keep-alive.scpt</string>
</array>
我不确定这是唯一的问题,但它肯定是一个问题。如果需要进一步调试,请尝试通过添加以下内容来捕获osascript
的错误输出:
<key>StandardErrorPath</key>
<string>/Users/xxx/Library/Logs/dropbox-keep-alive.err</string>
您要求launchd执行的脚本文件位于用户的/Library文件夹中。
Launchd没有访问该位置的权限。将其移动到/usr/local/sbin