math formulas

code

Friday, February 28, 2014

Getting the CPU load in a AutoHotKey / AHK script


I needed some way to easily obtain the load a specific process is causing, and I didn't want to use DLLs ore start other windows applications to get that data. So I did some digging and it turns out that wmic is able to report that percentage to you:

processtime( ProcessName )
{
  runwait %comspec% /c wmic path Win32_PerfFormattedData_PerfProc_Process.name='%ProcessName%' get percentprocessortime > c:\tmp\out.txt,,hide
  filereadline line, c:\tmp\out.txt, 2
  filedelete c:\tmp\out.txt
  return line
}


It definitely isn't pretty, and there are surely a lot of better solutions to this, but it gets the work done in a few lines. Also nice that AHK doesn't know how to capture the output of a console application.

No comments:

Post a Comment