Automation 使用apple脚本以指定的cpu%退出应用程序

Automation 使用apple脚本以指定的cpu%退出应用程序,automation,applescript,cpu-usage,Automation,Applescript,Cpu Usage,我想要一个脚本,它将在应用程序完成文件处理后退出。下面的代码是我通过研究其他人的作品来尝试自己创作的代码,但实际上没有成功。此特定软件不支持自动工作流,因此我能找到的唯一触发因素是cpu%,因为它在使用时可以使用高达100%,在空闲时可以使用低至1.3% getProcessPercentCPU("Mixed In Key 8") set someProcess to getProcessPercentCPU("Mixed In Key 8") on getProcessPercentCPU(s

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

如果有人能帮我把这个工作或有任何建议,将非常感谢。另外,我对应用程序描述还不熟悉。

您的应用程序描述基本正确,但在验证各部分是否正常工作之前,您似乎尝试了向前跳。如果您为处理程序和变量命名它们试图执行的操作,这也可能会有所帮助。例如,在本例中,您的处理程序似乎正在监视某个应用程序,然后在该应用程序的CPU使用率较低时退出该应用程序

请注意,在示例中,我已将流程名称更改为
任务纸
,因为我有可用的流程名称

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

我在每次重复时都添加了一个
延迟
,因为无休止的重复脚本本身往往会占用CPU。

基本上是正确的,但看起来在验证这些部分是否正常工作之前,您尝试了向前跳。如果您为处理程序和变量命名它们试图执行的操作,这也可能会有所帮助。例如,在本例中,您的处理程序似乎正在监视某个应用程序,然后在该应用程序的CPU使用率较低时退出该应用程序

请注意,在示例中,我已将流程名称更改为
任务纸
,因为我有可用的流程名称

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
我在每次重复时都添加了一个
延迟
,因为无休止地重复脚本本身往往会占用CPU