Read Hardware Sensors (Temperature, Voltage, Fanspeed)

Note: there was an error in the first post of the “get_number()” routine. It should have been “… / (2 ^ 16)”, not “… / (2 ^ 13)” as originally posted. The script has been corrected below.

This is a script inspired by a post on MacOSXHints.com (it’s a little kludgy to make the buttons wide so the text of the dialog doesn’t wrap):

property ascii_10 : ASCII character 10
property format_in_columns : false --change this to true to format into columns for use with monospaced font
property column_width : 50

set the_data to my get_HWSensor_data()
if button returned of (display dialog the_data buttons {"_          Copy to Clipboard          _", "_          OK          _"} default button 2 giving up after 30) contains "Clipboard" then set the clipboard to the_data

on get_HWSensor_data()
	try
		set hw_info to (do shell script "system_profiler SPHardwareDataType | awk '/Machine Name/ || /Machine Model/ || /CPU Type/' | sed 's/[^:]*: //' | awk '{ printf $0 \"  \" }'")
		set IOHWSensor_data to (do shell script "ioreg -n IOHWSensor")
		tell (a reference to my text item delimiters)
			set {old_tid, contents} to {contents, ascii_10}
			set {IOHWSensor_data, contents} to {(IOHWSensor_data's paragraphs) as Unicode text, old_tid}
		end tell
		set the_locations to (do shell script "echo " & quoted form of IOHWSensor_data & " | grep '\"location\"' | awk -F '\"' '{print $4}'")'s paragraphs
		set the_values to (do shell script "echo " & quoted form of IOHWSensor_data & " | grep '\"current-value\"' | awk -F ' = ' '{print $2}'")'s paragraphs
		set the_types to (do shell script "echo " & quoted form of IOHWSensor_data & " | grep '\"type\"' | awk -F '\"' '{print $4}'")'s paragraphs
		set all_data to {}
		repeat with i from 1 to (count the_locations)
			set this_type to (item i of the_types)
			set this_value to (item i of the_values)
			if this_type = "temperature" then
				set this_value to my get_temp_as_celcius(this_value)
			else if this_type = "voltage" then
				set this_value to my get_number(this_value) & " V" as Unicode text
			else if this_type = "fanspeed" then
				set this_value to my get_number(this_value) & " RPM" as Unicode text
			end if
			if format_in_columns then
				set end of all_data to my make_columns((item i of the_locations) & " " & this_type & ":", this_value)
			else
				set end of all_data to (item i of the_locations) & " " & this_type & ":" & tab & this_value & ascii_10
			end if
		end repeat
		return hw_info & ascii_10 & all_data as Unicode text
	on error the_error
		return "Could not get hardware sensor data. (" & the_error & ")"
	end try
end get_HWSensor_data

on make_columns(the_label, the_value)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, "."}
		set {value_count, contents} to {(count of (word 1 of text item 1 of the_value)), old_tid}
	end tell
	set total_count to (count the_label) + value_count
	set the_space to {}
	repeat with i from 1 to (column_width - total_count)
		set end of the_space to " "
	end repeat
	return {the_label, the_space, the_value, ascii_10} as Unicode text
end make_columns

on get_temp_as_celcius(this_value)
	set this_value to this_value as integer
	return ((((this_value / (2 ^ 13)) - ((this_value / (2 ^ 13)) mod 1)) / (2 ^ 3)) - 0.5) & "° C" as Unicode text
end get_temp_as_celcius

on get_number(this_value)
	set this_value to this_value as integer
	set this_value to ((round of ((this_value / (2 ^ 16)) * 100)) / 100) as Unicode text
	if this_value ends with ".0" then set this_value to text 1 thru -3 of this_value
	return this_value
end get_number

Jon

Model: PowerBook 15" 1.25GHz
AppleScript: 1.10
Browser: Safari 412.2
Operating System: Mac OS X (10.4)