Sie haben es im Grunde genommen richtig, aber es sieht so aus, als hätten Sie versucht, vor der Überprüfung zu springen, dass die Teile korrekt funktionieren. Es kann auch hilfreich sein, wenn Sie die Handler und Variablen benennen, was sie zu tun versuchen. In diesem Fall scheint es beispielsweise so zu sein, dass Ihr Handler eine App überwacht und diese App dann beendet, sobald sie eine geringe CPU-Auslastung erreicht.
Beachten Sie, dass ich den Prozessnamen in TaskPaper
in den Beispielen geändert habe, weil ich das zur Verfügung habe.
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
Zu diesem Zeitpunkt wissen wir zwei Dinge: dass die Shell-Skript, um die Zahl, die wir wollen zurückkehrt, und dass sie es als String ist zurück.
Um Zahlen zuverlässig zu vergleichen, müssen wir sie in numerische Werte umwandeln.
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
Dies funktioniert, aber es versucht, auch processToMonitor
sogar zu beenden, wenn processToMonitor
nicht ausgeführt wird.
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
Jetzt sind wir bereit, einen repeat
um den Handler hinzuzufügen:
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
ich eine delay
auf jeder Wiederholung hinzugefügt, da sich endlos wiederholenden Skripte oft eine CPU hog in sich selbst werden kann.