Processes cpu %

Hello,

Does anyone know how to set a variable to a process’s current CPU usage percentage. Ex. I am currently automating a template in apple motion. This program is not apple-scriptable so my script has no idea when it is done exporting a file. I am currently just setting my script to wait for a set duration but this can change from file to file so sometimes my script isn’t waiting long enough and sometimes it waits too long. I would like to have a repeat statment in my script that would wait until the Process “Motion” falls below %10 for example and then continues on. Some thing like this;

repeat
tell app “system events”
set process_cpu_usage to process “motion” cpu usage
if process_cpu_usage is < 10 then
exit repeat
end if
end tell
end repeat

If anyone knows how to do this it would be greatly appreciated.
Thanks in advance
Dallas

hI Dallasbr

This could possibly fit the bill

http://www.macosxhints.com/dlfiles/appInfoInTitle.applescript

this is pretty ugly but it might be what your after.

repeat
	set AppPercent to do shell script "top -FR -l2 | grep 'iTunes' | grep -v '0.0%' | cut -c 7-25"
	if AppPercent is greater than "iTunes       4.0% 1" then
		display dialog "you have gone above 4%   " & AppPercent
	else if AppPercent is less than "iTunes       3.5% 1" then
		display dialog "you have gone below 3.5%   " & AppPercent
	end if
end repeat

Alternatively:

repeat
	-- Maybe a short delay here?
	-- delay 5
	if getProcessPercentCPU("Motion") < 10 then exit repeat
end repeat

on getProcessPercentCPU(someProcess)
	do shell script "/bin/ps -xco %cpu,command | /usr/bin/grep '\\.[0-9] " & someProcess & "$' | /usr/bin/awk '{print $1}'"
	return result as number
end getProcessPercentCPU

Thanks alot guys, Bruce I tried yours out and it works exactly how I hoped.
Dallas

very useful routine, Bruce.
A little note: coercion to number depends on the international number format settings.
It doesn’t work, if a comma is used as decimal point (like in Gemany)
Coercing to real doesn’t work either, if the text class is string,
but in this case (a shell script returns Unicode text) it works


	return result as real

I remember seeing that come up before; Thanks for the reminder. :slight_smile:

cheers Bruce, Stefan learned quite a bit :smiley:

What did I learn? :wink:

that I have a lot to learn :slight_smile: