【问题标题】:using apple script to exit app at a specified cpu%使用苹果脚本在指定的 cpu% 处退出应用程序
【发布时间】:2017-02-22 17:47:31
【问题描述】:

我想要一个在完成文件处理后退出应用程序的脚本。下面的代码是我尝试通过研究其他创作来创建自己的代码,但实际上并没有让它发挥作用。这个特定的软件不支持自动化工作流程,因此我能找到的唯一触发因素是通过 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的新手。

【问题讨论】:

    标签: automation applescript cpu-usage


    【解决方案1】:

    您基本上是正确的,但看起来您在验证这些部分是否正常工作之前试图跳到前面。如果您将处理程序和变量命名为它们正在尝试执行的操作,它也可能会有所帮助。例如,在这种情况下,您的处理程序似乎正在监视一个应用程序,然后在该应用程序达到低 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2021-03-23
      • 2017-12-26
      • 1970-01-01
      相关资源
      最近更新 更多