{"id":54973,"date":"2008-10-27T11:14:00","date_gmt":"2008-10-27T11:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/10\/27\/hey-scripting-guy-how-can-i-create-an-hta-to-do-temperature-conversions\/"},"modified":"2008-10-27T11:14:00","modified_gmt":"2008-10-27T11:14:00","slug":"hey-scripting-guy-how-can-i-create-an-hta-to-do-temperature-conversions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-an-hta-to-do-temperature-conversions\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create an HTA to Do Temperature Conversions?"},"content":{"rendered":"<h2><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/h2>\n<p>Hey, Scripting Guy! I read with interest your &#8220;blog from down under,&#8221; 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&#8217;s laptop. Can you change the Windows PowerShell script into an HTA that would be easy to use?<\/p>\n<p>&#8211; MH<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hi MH,<\/p>\n<p>You do not want to install Windows PowerShell on your wife\u2019s laptop? I thought that was what the scripting wife&#8217;s laptop was for\u2014as a test bed for learning new applications. The scripting wife&#8217;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).<\/p>\n<p>I really believe your wife would thank you if you upgraded her to Windows PowerShell. However, I really don&#8217;t do marriage counseling anymore, but I do still dabble from time to time. Remind me to show you the &#8220;Love-O-Matic&#8221;\u2014it&#8217;s like the Script-O-Matic I wrote, only it sends little love messages to the script wife via e-mail. It\u2019s part of our \u201cBetter living through scripting\u201d series of scripts. Maybe I will show you the Love-O-Matic for Valentine&#8217;s Day.<\/p>\n<p>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&nbsp;go:<\/p>\n<pre class=\"codeSample\">&lt;head&gt;\n&lt;title&gt;Convert Temperatures&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=\"objTest\" \n     APPLICATIONNAME=\"Convert Temp\"\n     SCROLL=\"yes\"\n     SINGLEINSTANCE=\"yes\"\n&gt;\n&lt;\/head&gt;\n&lt;SCRIPT LANGUAGE=\"VBScript\"&gt;\n' PUT YOUR SUBROUTINES HERE\nSub SubChoose\n        For Each objButton in RadioOption\n            If objButton.Checked Then\n                Select Case objButton.Value\n                \tCase 1\n                \tstrString = \"the temperature is \" &amp; funCelsius(txtBox.value)\n                \tDataArea.innerHTML = strString\n                \tCase 2\n                \tstrString = \"The temperature is \" &amp; funFahrenheit(txtBox.value)\n                \tDataArea.innerHTML = strString\n                \tCase Else\n                \tMsgBox \"who knows\"\n                End Select\n            End If\n        Next\nEnd Sub\nFunction funCelsius(strIn)\nfunCelsius = formatNumber((strIN * 1.8) + 32)\nEnd Function \nFunction funFahrenheit(strIn)\nfunFahrenheit = formatNumber((5\/9) * (strIN - 32))\nEnd Function \n&lt;\/SCRIPT&gt;\n&lt;body&gt;\n&lt;input type=\"text\" name=\"TxTbox\" size=\"30\" value=72&gt; Temperature to convert&lt;br&gt;\n\t&lt;p&gt;\n\t&lt;input type=\"radio\" name=\"RadioOption\" value=1&gt;Fahrenheit&lt;BR&gt;\n    &lt;input type=\"radio\" name=\"RadioOption\" value=2&gt;Celsius&lt;BR&gt;\n    &lt;input id=runbutton  class=\"button\" type=\"button\" value=\"Convert\"_\n    \t name=\"run_button\"  onClick=\"SubChoose\"&gt;\n&lt;p&gt;\n&lt;Span ID = \"DataArea\"&gt;&lt;\/Span&gt;\n&lt;\/body&gt;<\/pre>\n<p>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 <b>yes<\/b>. This section of the script is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">&lt;head&gt;\n&lt;title&gt;Convert Temperatures&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=\"objTest\" \n     APPLICATIONNAME=\"Convert Temp\"\n     SCROLL=\"yes\"\n     SINGLEINSTANCE=\"yes\"\n&gt;\n&lt;\/head&gt;<\/pre>\n<p>Once we are finished with the head section of the script, we specify the language for our application\u2014VBScript in this case\u2014by using this tag:<\/p>\n<pre class=\"codeSample\">&lt;SCRIPT LANGUAGE=\"VBScript\"&gt;<\/pre>\n<p>Now we create a subroutine that is used to evaluate the buttons that are selected. If you click <b>Celsius<\/b>, the temperature will be converted from Fahrenheit to Celsius (case 1). The value is taken from the text box and passed to the <b>funCelsius<\/b> function to do the conversion. If you click <b>Fahrenheit<\/b>, the temperature will be converted from Celsius to Fahrenheit (the case 2). The value is passed from the text box to the <b>funFahrenheit<\/b> function. In either case, we write the value of the <b>strString<\/b> to the <b>innerHTML<\/b> property of the <b>DataArea<\/b>. If neither case is selected, we have a case <b>else<\/b> that simply displays a message that says, &#8220;Who Knows.&#8221; As the script is written, it is very unlikely to reach the case <b>else<\/b>. The <b>SubChoose<\/b> subroutine is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">Sub SubChoose\n\n        For Each objButton in RadioOption\n            If objButton.Checked Then\n                Select Case objButton.Value\n                \tCase 1\n                \tstrString = \"the temperature is \" &amp; funCelsius(txtBox.value)\n                \tDataArea.innerHTML = strString\n                \tCase 2\n                \tstrString = \"The temperature is \" &amp; funFahrenheit(txtBox.value)\n                \tDataArea.innerHTML = strString\n                \tCase Else\n                \tMsgBox \"who knows\"\n                End Select\n            End If\n        Next\nEnd Sub<\/pre>\n<p>We now have two functions that end the script section of the HTA. The first function is called <b>funCelsius<\/b>, which is called by passing an integer to it. We then use the <b>FormatNumber<\/b> function that is built into VBScript to display the number to two decimal points. By default, <b>FormatNumber<\/b> displays two decimal points. We take the value passed to the <b>funcelsius<\/b> function, multiply it by 9\/5, and then add 32. This will convert Celsius to Fahrenheit. The function is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">Function funCelsius(strIn)\nfunCelsius = formatNumber((strIN * 1.8) + 32)\nEnd Function<\/pre>\n<p>The second function is very similar. We pass a value to the <b>funFahrenheit<\/b> function. The parentheses require that 32 be subtracted from <b>strIN<\/b> with the result multiplied by 5\/9. We use the VBScript <b>FormatNumber<\/b> function to display only two decimal places. This function is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">Function funFahrenheit(strIn)\nfunFahrenheit = formatNumber((5\/9) * (strIN - 32))\nEnd Function <\/pre>\n<p>We need to end the script section by closing the script tag as shown here:<\/p>\n<pre class=\"codeSample\">&lt;\/SCRIPT&gt;<\/pre>\n<p>The body section of the HTA script is used to create the form we see. We first create a text box named <b>Txtbox<\/b>, 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 <b>select<\/b> case section we looked at earlier. If the second option button is clicked, we set the value to 2. When the <b>Convert<\/b> button is pressed, it generates an <b>onClick<\/b> event, and we then call the <b>SubChoose<\/b> subroutine, which will then perform our calculations. The other thing we do is create a <b>DataArea<\/b> span that will contain the text we write to the application from within our subroutine. This section of the script is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">&lt;body&gt;\n&lt;input type=\"text\" name=\"TxTbox\" size=\"30\" value=72&gt; Temperature to convert&lt;br&gt;\n\t&lt;p&gt;\n\t&lt;input type=\"radio\" name=\"RadioOption\" value=1&gt;Fahrenheit&lt;BR&gt;\n    &lt;input type=\"radio\" name=\"RadioOption\" value=2&gt;Celsius&lt;BR&gt;\n    &lt;input id=runbutton  class=\"button\" type=\"button\" value=\"Convert\"_\n    \t name=\"run_button\"  onClick=\"SubChoose\"&gt;\n&lt;p&gt;\n&lt;Span ID = \"DataArea\"&gt;&lt;\/Span&gt;\n&lt;\/body&gt;<\/pre>\n<p>The Convert Temperatures HTA is seen here:<\/p>\n<p><img decoding=\"async\" height=\"203\" alt=\"The Convert Temperatures HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1027\/ConvertTemperaturesHTA.jpg\" width=\"450\" border=\"0\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>To use the Convert Temperatures HTA, all you do is type the temperature value in the <b>Temperature to convert<\/b> box. Click either the <b>Fahrenheit<\/b> or the <b>Celsius<\/b> option button, and then click <b>Convert<\/b>. As you can see above, 72 degrees Fahrenheit is 22.22 degrees Celsius.<\/p>\n<p>MH, I hope this helps you to have fun on your vacation to Europe. Take lots of pictures.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I read with interest your &#8220;blog from down under,&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-54973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I read with interest your &#8220;blog from down under,&#8221; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=54973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}