Hey, Scripting Guy! How Can I Create an HTA to Do Temperature Conversions?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I read with interest your “blog from down under,” and I was interested in your function that converts Fahrenheit to Celsius. My wife and I are going on vacation to Germany, and I want to be able to do metric conversions. The problem, however, is that I do not want to install Windows PowerShell on my wife’s laptop. Can you change the Windows PowerShell script into an HTA that would be easy to use?

– MH

SpacerHey, Scripting Guy! Answer

Hi MH,

You do not want to install Windows PowerShell on your wife’s laptop? I thought that was what the scripting wife’s laptop was for—as a test bed for learning new applications. The scripting wife’s laptop is currently running the community technology preview of Windows 7. She loves being on the forefront of technology. Her scripting computer in the kitchen is running the beta of Internet Explorer 8 (which she absolutely loves by the way).

I really believe your wife would thank you if you upgraded her to Windows PowerShell. However, I really don’t do marriage counseling anymore, but I do still dabble from time to time. Remind me to show you the “Love-O-Matic”—it’s like the Script-O-Matic I wrote, only it sends little love messages to the script wife via e-mail. It’s part of our “Better living through scripting” series of scripts. Maybe I will show you the Love-O-Matic for Valentine’s Day.

Okay, MH, so you want a good old-fashioned HTML application (HTA) file that you can simply double-click and run to do temperature conversion? Happy to oblige. Here you go:

<head>
<title>Convert Temperatures</title>
<HTA:APPLICATION 
     ID="objTest" 
     APPLICATIONNAME="Convert Temp"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>
<SCRIPT LANGUAGE="VBScript">
' PUT YOUR SUBROUTINES HERE
Sub SubChoose
        For Each objButton in RadioOption
            If objButton.Checked Then
                Select Case objButton.Value
                	Case 1
                	strString = "the temperature is " & funCelsius(txtBox.value)
                	DataArea.innerHTML = strString
                	Case 2
                	strString = "The temperature is " & funFahrenheit(txtBox.value)
                	DataArea.innerHTML = strString
                	Case Else
                	MsgBox "who knows"
                End Select
            End If
        Next
End Sub
Function funCelsius(strIn)
funCelsius = formatNumber((strIN * 1.8) + 32)
End Function 
Function funFahrenheit(strIn)
funFahrenheit = formatNumber((5/9) * (strIN - 32))
End Function 
</SCRIPT>
<body>
<input type="text" name="TxTbox" size="30" value=72> Temperature to convert<br>
	<p>
	<input type="radio" name="RadioOption" value=1>Fahrenheit<BR>
    <input type="radio" name="RadioOption" value=2>Celsius<BR>
    <input id=runbutton  class="button" type="button" value="Convert"_
    	 name="run_button"  onClick="SubChoose">
<p>
<Span ID = "DataArea"></Span>
</body>

We begin the script by setting the head section of the script. In the head section we specify the title of the application, an ID, and the application name. We also determine if the application can scroll and if only one instance of it is allowed to run at a time. We set both of these values to yes. This section of the script is seen here:

<head>
<title>Convert Temperatures</title>
<HTA:APPLICATION 
     ID="objTest" 
     APPLICATIONNAME="Convert Temp"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
>
</head>

Once we are finished with the head section of the script, we specify the language for our application—VBScript in this case—by using this tag:

<SCRIPT LANGUAGE="VBScript">

Now we create a subroutine that is used to evaluate the buttons that are selected. If you click Celsius, the temperature will be converted from Fahrenheit to Celsius (case 1). The value is taken from the text box and passed to the funCelsius function to do the conversion. If you click Fahrenheit, the temperature will be converted from Celsius to Fahrenheit (the case 2). The value is passed from the text box to the funFahrenheit function. In either case, we write the value of the strString to the innerHTML property of the DataArea. If neither case is selected, we have a case else that simply displays a message that says, “Who Knows.” As the script is written, it is very unlikely to reach the case else. The SubChoose subroutine is seen here:

Sub SubChoose

        For Each objButton in RadioOption
            If objButton.Checked Then
                Select Case objButton.Value
                	Case 1
                	strString = "the temperature is " & funCelsius(txtBox.value)
                	DataArea.innerHTML = strString
                	Case 2
                	strString = "The temperature is " & funFahrenheit(txtBox.value)
                	DataArea.innerHTML = strString
                	Case Else
                	MsgBox "who knows"
                End Select
            End If
        Next
End Sub

We now have two functions that end the script section of the HTA. The first function is called funCelsius, which is called by passing an integer to it. We then use the FormatNumber function that is built into VBScript to display the number to two decimal points. By default, FormatNumber displays two decimal points. We take the value passed to the funcelsius function, multiply it by 9/5, and then add 32. This will convert Celsius to Fahrenheit. The function is seen here:

Function funCelsius(strIn)
funCelsius = formatNumber((strIN * 1.8) + 32)
End Function

The second function is very similar. We pass a value to the funFahrenheit function. The parentheses require that 32 be subtracted from strIN with the result multiplied by 5/9. We use the VBScript FormatNumber function to display only two decimal places. This function is seen here:

Function funFahrenheit(strIn)
funFahrenheit = formatNumber((5/9) * (strIN - 32))
End Function 

We need to end the script section by closing the script tag as shown here:

</SCRIPT>

The body section of the HTA script is used to create the form we see. We first create a text box named Txtbox, set its size and default value. We also add text at the end of the text box to tell us what the text box is used for. We then create two options buttons. When the first option button is clicked, we set the value to 1. This is what we evaluate in our select case section we looked at earlier. If the second option button is clicked, we set the value to 2. When the Convert button is pressed, it generates an onClick event, and we then call the SubChoose subroutine, which will then perform our calculations. The other thing we do is create a DataArea span that will contain the text we write to the application from within our subroutine. This section of the script is seen here:

<body>
<input type="text" name="TxTbox" size="30" value=72> Temperature to convert<br>
	<p>
	<input type="radio" name="RadioOption" value=1>Fahrenheit<BR>
    <input type="radio" name="RadioOption" value=2>Celsius<BR>
    <input id=runbutton  class="button" type="button" value="Convert"_
    	 name="run_button"  onClick="SubChoose">
<p>
<Span ID = "DataArea"></Span>
</body>

The Convert Temperatures HTA is seen here:

The Convert Temperatures HTA

 

To use the Convert Temperatures HTA, all you do is type the temperature value in the Temperature to convert box. Click either the Fahrenheit or the Celsius option button, and then click Convert. As you can see above, 72 degrees Fahrenheit is 22.22 degrees Celsius.

MH, I hope this helps you to have fun on your vacation to Europe. Take lots of pictures.

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon