Wednesday, October 23, 2013

CPU an Ram Meter Geeklet


Difficulty: Medium

OS X Mavericks Update:

The RAM Script has changed, plz Update!

This Geeklet shows a radial CPU and/or Free RAM Indicator.



It tries to draw the Pie Chart with just a single Char, but for this you need to install a specific Font that contains no regular Chars but Pie Chart Symbols to draw the Result

So lets get started:

1. Download the Font needed for the Pie Chart Grapics here:

Download Me


Once downloaded, double click it to install, an OS X Installer opens and asks you to install, do it.

2. Drag an Image Geeklet onto your Desktop and link it to this Image to have a Default Background Image if wanted, this Part is your Choice:


3. Now you need a Script Geeklet, add the following Code for the CPU Meter:


ruby

def ram 
 myCPU=`top -l 1 | awk '/CPU usage/ {print $3}' | sed s/%//`
 return myCPU
end 

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end
def label
 run = system("osascript -e 'tell application \"GeekTool Helper\" to set Command of first item of (geeklets whose name is \"cpu\") to \"echo R\"'")
end

def label2(res)
 run = system("osascript -e 'tell application \"GeekTool Helper\" to set Command of first item of (geeklets whose name is \"cpu\") to \"echo "+res.to_s+"%\"'")
end

label

myCpu = ram.to_i
currCpu1 = (myCpu).percent_of(100)    # => 10.0  (%)
currCpu2 = currCpu1 / 5

realCpu = case currCpu2.floor
   when 0 then "a"
   when 1 then "b"
   when 2 then "c"
   when 3 then "d"
   when 4 then "e"
   when 5 then "f"
   when 6 then "g"
   when 7 then "h"
   when 8 then "i"
   when 9 then "j"
   when 10 then "k"
   when 11 then "l"
   when 12 then "m"
   when 13 then "n"
   when 14 then "o"
   when 15 then "p"
   when 16 then "q"
   when 17 then "r"
   when 18 then "s"
   when 19 then "t"
   else "u"
end

puts realCpu

label2(myCpu.floor)

 For this Geeklet its important to have a really large Font Size, select the previously installed Font and choose a large Font, i use a 120px Font Size here, also set a refresh Rate of every 5 seconds, faster is not needed, its ok, and less CPU hungry i guess

As you can see in the Code above, it uses Applescript to write the Percentage into another Geeklet, for this:

4. Create another Script Geeklet and enter a Name in its Settings, call it cpu, adjust the font and size to your wishes and place it centered inside the CPU Meter we created in Step 3.

This was the job, now you have the CPU Meter up and running on your desktop

The Free RAM Meter

The RAM Meter is done nearly the same as to CPU Meter, only change is the Code :


ruby

def ram 
 myRAM=`top -l 1 -F -R | awk '/PhysMem/'`
 return myRAM
end 

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

def label(count)
 run = system("osascript -e 'tell application \"GeekTool Helper\" to set Command of first item of (geeklets whose name is \"ram\") to \"echo "+count.to_s+ "%\"'")
end

totalRAM=8192

tmp = ram.split(" ")
tmp2 = (tmp[1]).to_i
myRam = tmp2

currRam1 = (myRam).percent_of(totalRAM)    # => 10.0  (%)
currRam2 = currRam1 / 5

realRam = case currRam2.floor
   when 10 then "k"
   when 11 then "l"
   when 12 then "m"
   when 13 then "n"
   when 14 then "o"
   when 15 then "p"
   when 16 then "q"
   when 17 then "r"
   when 18 then "s"
   when 19 then "t"
   else "u"
end
puts realRam
label(currRam1.floor)


As before, set a Refresh of every 5 seconds, that is fast enough, but while this is RAM, you can update every 60 seconds as well, its better i guess, needs less recources

And finally drag another Dummy Script Geeklet and call it ram, in my Case, i made the Background White and moved it to the left edge of the Circle, check the Screenshot above to see the Effect

And thats all!


No comments:

Post a Comment