AppleScript名:使用CPUの状況を取得 |
— Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aDict to retCpuUsage() of me –> {userCPU:3.2, sysCPU:12.29, idleCPU:84.49} on retCpuUsage() set cpuUseRes to do shell script "top -l 1 | head -10 | grep ’CPU usage’" set aRes to (parseStrFromTo(cpuUseRes, " ", "%") of me) set userUsage to (last item of (words of item 1 of aRes)) as real set sysUsage to (item 2 of aRes) as real set idleUsage to (item -2 of aRes) as real return {userCPU:userUsage, sysCPU:sysUsage, idleCPU:idleUsage} end retCpuUsage on parseStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) — terminate check, return the result (aDict) to caller set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) — skip over separator theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 — skip over separator theScanner’s scanString:toStr intoString:(missing value) anArray’s addObject:theValue end repeat return anArray as list end parseStrFromTo |
More from my site
(Visited 148 times, 1 visits today)