您可能会考虑制作一个“保持开放Applescript”应用程序,该应用程序根据正在运行的进程的存在来设置/重置睡眠计时器。创建启动的plist也是可行的解决方案,但是我仍然对语法感到困惑。“ pmset force sleep X”命令不需要root用户访问权限,但是设置会在重新启动时重置。
由于您的情况听起来似乎无法满足您的所有需求,因此我会为您提供一些建议。
property LoopTime: 5 --measured in seconds
property normalSleepTimeout: 30 --measured in minutes
property processName: "rsync" --the name of the process you're trying to find
on run
do shell script "pmset force sleep 0" --disables sleep
idle()
end
on idle
if not appIsRunning() then
do shell script ("pmset force sleep " & normalSleepTimeout as string) -- sets sleep to desired idle timeout
quit
end
return
end
on appIsRunning()
--Here's where you need to do the test that an app is running. True needs to mean "the app is running". Store the value to "result" or change the below return statement.
return result
end
对于诸如rsync和后台进程之类的事情,您将需要变得更聪明并轮询$ top之类的其他功能。
set result to False
if 0 < (count of (do shell script ("top -l 1 | grep" & processName as string))) then
set result to True
end
请注意,在上述情况下,如果rsyncd正在运行,则仅搜索“ rsync”将返回假肯定,因为“ rsync”和“ rsyncd”都匹配。如果这不适合您,则可能需要变得更加棘手。
如果该应用程序是一个Windowed进程,那么我将使用以下命令确定正在运行的程序:
tell application "System Events" to set RunningAppNames to name of processes
或用于捆绑包标识符(更精确)
tell application "System Events" to set RunningBundles to bundle identifier of processes
告诉我更多有关您的方案的信息,我将尝试用更灵活的用户界面编写更精确的内容。