我想要一个在完成处理文件后退出应用程序的脚本。下面的代码是我尝试通过研究其他创作创建自己的代码,但实际上没有运气实现它。这个特定的软件剂量不支持自动化工作流程,因此我能找到的唯一触发器是cpu%,因为它在使用时可以使用高达100%,在闲置时可以使用高达1.3%
getProcessPercentCPU("Mixed In Key 8")
set someProcess to getProcessPercentCPU("Mixed In Key 8")
on getProcessPercentCPU(someProcess)
repeat
do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & someProcess & "$/ {print $1}'"
if someProcess is less than "2.0" then
application "Mixed In Key 8" quit
end if
end repeat
end getProcessPercentCPU
如果有人可以帮助我开展这项工作,或者有任何非常感激的建议。我也不熟悉applescripting。
答案 0 :(得分:0)
你已经基本正确了,但看起来你试图在确认这些部分正常工作之前先跳过去。如果您将处理程序和变量命名为他们正在尝试执行的操作,那么它也可能会有所帮助。例如,在这种情况下,您的处理程序似乎正在监视应用程序,然后在达到低CPU使用率时退出该应用程序。
请注意,我已在示例中将流程名称更改为TaskPaper
,因为我已将其更改为。
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
display dialog processCPU
end quitOnLowCPU
此时,我们知道两件事:shell脚本返回我们想要的数字,并且它将它作为字符串返回。
为了可靠地比较数字,我们需要将它们转换为数值。
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
end quitOnLowCPU
这样做有效,但即使processToMonitor
未运行,它也会尝试退出processToMonitor
。
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
if processCPU is "" then
--the process is gone. We're done
return
end if
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
end quitOnLowCPU
现在我们准备在处理程序周围添加repeat
:
quitOnLowCPU("TaskPaper")
on quitOnLowCPU(processToMonitor)
repeat
set processCPU to do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processToMonitor & "$/ {print $1}'"
if processCPU is "" then
--the process is gone. We're done
return
end if
--convert the shell script response string to a number
set processCPU to processCPU as number
--compare to the threshold of quitting
if processCPU is less than 2.0 then
tell application processToMonitor to quit
end if
delay 1
end repeat
end quitOnLowCPU
我在每次重复时添加了delay
因为无休止地重复脚本本身就会成为CPU占用者。