Is there a way to get the CPU(s) temperature?

Is there a way to get the CPU(s) temperature?

Maybe with some additional Objective-C includes if there’s no clean AS way?

Fredi

The widget “miniCpuHeat” seems to use a Javascript way to get the heat!
Maybe you can find out where it gets the info from :rolleyes:

Thx. Will have a look …

Fredi

it is actually a perl script that detects the temp (sensors.pl)

well to be a bit more helpful, copy that file to your home directory and in terminal type “perl sensors.pl” and hit return

in AS

do shell script "perl sensors.pl"

I’m sure there are better ways of doing this but try:

property return_f : false

try
	set the_result to (do shell script "ioreg -c IOHWSensor | grep -vE '\\{|\\}|\\+\\-o'")'s paragraphs
on error
	return "unknown"
end try
set the_temps to {}
repeat with i from 1 to (count the_result)
	try
		set this_value to item i of the_result
		set this_type to item (i + 2) of the_result
		set this_key to item (i + 3) of the_result
		if (this_value contains "current-value") and (this_type contains "CPU") and (this_key contains "temperature") then
			set end of the_temps to this_value's word -1 as integer
		end if
	on error
		exit repeat
	end try
end repeat
set the_sum to 0
repeat with i in the_temps
	set the_sum to the_sum + ((i's contents) / (2 ^ 16))
end repeat
set this_temp to the_sum / (count the_temps)
if return_f then
	return ((this_temp * (9 / 5) + 32) as Unicode text) & "°F"
else
	return (this_temp as Unicode text) & "°C"
end if

Jon

Thx a lot everyone. :slight_smile:

Fredi

I just moved to my dual G5 and your script gave me a “division by 0” error. I cahnged the code a bit to solve that problem, but it looks like it can’t get the temperature of my G5. Guess the problem has something to do with having multiple CPU’s. Any ideas how I could solve this?

Fredi

Yes, I tried this on my MacBook Pro, and initially got the division by zero error. Logically, this is because the results of the shell script does not contain “CPU” and “temperature”, so the list the_temps is empty and so count the_temps = 0.

Here is the_result:

{" | | | | | "polling-period" = 18446744073709551615", " | | | | | "current-value" = 6553600", " | | | | | "location" = "GPU"“, " | | | | | "type" = "gpu"”, " | | | | | "CFBundleIdentifier" = "com.apple.driver.AppleHWSensor"“, " | | | | | "IOClass" = "IOHWSensor"”, " | | | | | "version" = 2", " | | | | | "zone" = <00000002>“, " | | | | | "IOProbeScore" = 0”, " | | | | | "IOMatchCategory" = "IODefaultMatchCategory"“, " | | | | | "polling-period-ns" = 18446744073709551615”, " | | | | | "IOProviderClass" = "IOService"“, " | | | | | "sensor-id" = 6”, " | | | | | "}

This works on ibook. Just turn it into an Applescript. When you get the values then divide by 65536 and that gives C temp. Then take that # times 9/5+32 and you’ll have F temp.

#!/bin/bash
ioreg -l | grep “current-value”
ioreg -l | grep “hwsensor-location”

This may break on other macs, and I’m sure it could be a little more elegant, but works so far on my dual G5.


set the_result to (do shell script "ioreg -c IOHWSensor | grep -vE '\\{|\\}|\\+\\-o'")'s paragraphs

set all_display to ""
repeat with i from 0 to 16
	
	set jump to 14
	set the_location to item (3 + (jump * i)) of the_result
	set the_location to characters 41 thru ((count of characters of the_location) - 1) of the_location as string
	set the_type to item (4 + (jump * i)) of the_result
	set the_text to item (2 + (jump * i)) of the_result as string
	set the_text to characters 44 thru (count of characters of the_text) of the_text as string --(length of item 2 of the_result)
	set the_type to characters 37 thru ((count of characters of the_type) - 1) of the_type as string
	if the_type = "temperature" then
		set all_display to all_display & "
" & the_location & ": " & ((the_text / 65536) * (9 / 5)) + 32 & " F" as string
	end if
end repeat

display dialog all_display


results…

DRIVE BAY: 89.6 F
BACKSIDE: 129.2 F
U3 HEATSINK: 168.125 F
CPU A AD7417 AMB: 139.55 F
CPU B AD7417 AMB: 130.1 F

-Jeff Jones