GUI Scripting: Can't read text field

Hi there,

a few days ago I purchased a PC Sensor’s gold TEMPer (cheap USB thermometer device).

Since I was not not able to find a working shell tool for this device that works under OS X I tried to use the only working graphical tool TEMPer1X (http://ssl-lab.blogspot.de/2012/02/controlling-your-temper-on-mac-os-x.html)
and get the temp values with GUI scripting.

So far I was able to use the READ button but had no success to read the text field to get the temp. :frowning:
Do you have any hints for me???

Here is my script part and the info I found with UIElementInspector and “get every UI element”

Many thanks… WürfelMac


tell application "System Events" to set frontmost of process "TEMPer1X" to true

set TEMP to "text"
tell application "System Events"
	tell process "TEMPer1X"
		tell window "TEMPer1X"
			click button "Read"  -- this works, the temp appears in the before empty text field
			-- Try 1:  set TEMP to value of text field 1
			-- Try 2:  set TEMP to value of attribute "AXStaticText" of window "TEMPer1X"
			if attribute "AXIdentifier" of UI element is "_NS:28" then set TEMP to value of UI element  -- Try 3
			get TEMP
		end tell
	end tell
end tell

UIElementInspector (Temp-Field):

<AXApplication: “TEMPer1X”>
<AXWindow: “TEMPer1X”>

Attributes:
AXRole: “AXStaticText”
AXRoleDescription: “text”
AXHelp: “(null)”
AXValue: “27,125”
AXEnabled: “1”
AXFocused: “1”
AXParent: “<AXWindow: “TEMPer1X”>”
AXWindow: “<AXWindow: “TEMPer1X”>”
AXTopLevelUIElement: “<AXWindow: “TEMPer1X”>”
AXPosition: “x=1073 y=120”
AXSize: “w=96 h=22”
AXChildren: “<array of size 0>”
AXSelectedText: “27,125”
AXSelectedTextRange (W): “pos=0 len=6”
AXNumberOfCharacters: “6”
AXVisibleCharacterRange (W): “pos=0 len=6”
AXInsertionPointLineNumber: “(null)”
AXPlaceholderValue: “(null)”
AXIdentifier: “_NS:28”

Actions:
AXShowMenu - show menu

get every UI element:

button 1 of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
button 2 of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
button 3 of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
UI element 4 of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
button “Read” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “25,25” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “0” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “-20” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “20” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “40” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “60” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “80” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “100” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”
static text “TEMPer1X” of window “TEMPer1X” of application process “TEMPer1X” of application “System Events”}

Hi.

Your code doesn’t specify which UI element is meant. (‘UI element’ is a generic term covering all classes of object in the window.)

I don’t have the device, but as far as I can tell, ‘UI element 4’ is the thermometer bar across the top and ‘static text 1’ is the text in the pseudo field in the bottom left-hand corner. I imagine the ‘value’ of either of them would suit you. You may need a short delay between clicking the button and getting the new value.


tell application "System Events" to set frontmost of process "TEMPer1X" to true

set TEMP to "text"
tell application "System Events"
	tell application process "TEMPer1X"
		tell window "TEMPer1X"
			click button "Read" -- this works, the temp appears in the before empty text field
			set TEMP to value of UI element 4 -- Value of thermometer bar.
			-- Or:
			set TEMP to value of static text 1 -- Value of "text field".
		end tell
	end tell
end tell

Hi Nigel,

MANY THANKS!!! for your replay.

Both of your solutions work like a charm :smiley:
Now I can start playing a bit with this device.

Best regards an a nice weekend! WürfelMac

Here is the complete script in case somebody likes / needs it.

It checks if GUI scripting is enabled, opens the GUI app, grabs the temperature, closes the app and write the temp to a log file next to the app.

Unfortunately the GUI app TEMPer1X only works with 10.7 and 10.8.

I tried to recompile it under 10.6 and it works but crashes after a second.
It seems it crashes too quick for the script before it can grab the temp.

Thanks again, have a nice weekend… WürfelMac


tell application "System Events" to set isUIScriptingEnabled to UI elements enabled
if isUIScriptingEnabled = false then
	tell application "System Preferences"
		activate
		set current pane to pane "com.apple.preference.universalaccess"
		display dialog "Your system is not properly configured to run this script.  
         Please select the \"Enable access for assistive devices\" 
         checkbox and trigger the script again to proceed."
		return
	end tell
end if


tell application "TEMPer1X" to activate

set TEMP to "text"
tell application "System Events"
	tell application process "TEMPer1X"
		tell window "TEMPer1X"
			click button "Read"
			set TEMP to value of static text 1 --# Value of "text field"
		end tell
	end tell
end tell

tell application "TEMPer1X" to quit


--  # Get Path to the Script and set this as path for the Log
set ScriptLocation to (path to me)
tell application "Finder"
	set ScriptPath to (container of ScriptLocation) as string
end tell

set TempLog to (open for access file (ScriptPath & "TempLog.txt") with write permission)
set LogLine to short date string of (current date) & tab & time string of (current date) & tab & TEMP & " °C" & return
write LogLine to TempLog starting at eof
close access TempLog