applescript - do shell output to variable is blank Hi I'm trying to get the cpuValue but I get &quot;&quot; for the variable.
set cpuValue to &quot;&quot;
getProcessPercentCPU(&quot;Safari&quot;)

on getProcessPercentCPU(someProcess)
    set cpuValue to do shell script &quot;/bin/ps -xco %cpu,command | /usr/bin/awk '/&quot; &amp; someProcess &amp; &quot;$/ {print $1}'&quot;
end getProcessPercentCPU

log &quot;output:&quot; &amp; cpuValue

Output
&quot;&quot;
AppleScript treats variables created with set as local by default (although the documentation isn't totally clear IMO). This means that the cpuValue variable inside on getProcessPercentCPU is local to that handler, and different from the variable with the same name in the outer scope.
Essentially, you have two different variables that just happen to have the same name, one set to &quot;&quot;, the other to the CPU percentage for the Safari process.
You could solve this by adding global cpuValue inside the handler, but returning values in global variables isn't good programming practice. It's almost always better to return values like a function:
set cpuValue to getProcessPercentCPU(&quot;Safari&quot;)
    
on getProcessPercentCPU(someProcess)
    set processPercentCPU to do shell script &quot;/bin/ps -xco %cpu,command | /usr/bin/awk '/&quot; &amp; someProcess &amp; &quot;$/ {print $1}'&quot;
    return processPercentCPU
end getProcessPercentCPU
    
log &quot;output:&quot; &amp; cpuValue