Using "HTA input forms" in Batch files
Posted: 29 Jul 2015 20:54
Brief introduction: the first time I saw a Batch-HTA hybrid script I was very confused. I didn't knew what HTA was (and certainly the example didn't explained it). Although there are several examples in this site on different ways to write a Batch-HTA script that works, I just didn't fathomed out the purpose of such methods. I always had thought that the usefulness of a Batch-XYZ hybrid script depends on the features that the XYZ part provide to the Batch part. If the Batch part is used just to start the XYZ part, then it is really a pure XYZ application that don't require any hybrid script. I never had seen before an example of an useful Batch-HTA hybrid script, even nor a pure HTA application (the few data I knew about it is that HTA have the same capabilities of an .HTML web page).
Then, I saw this request in SO site where the user wants to show something similar to a part of a web page and "get input" from it into a Batch file. I remembered that "HTA files are similar to web pages" and that is possible to write a Batch-HTA hybrid script, so I googled for this topic and found this MS site that explains the details about .HTA files. After some research I wrote the program below that, in my opinion, is the first useful example of a Batch-HTA hybrid script.
I think that the method used in this program is both obvious and simple. Of course, there are many different possibilities of use this technique; for example, radio buttons:
... and a very long et cetera! I copied the image of the Start screen in my computer (called "Inicio" in Spanish) and got this file:
I wrote the text file below in order to subdivide the image in parts using the format allowed by my TextToHtml.bat program (http://www.dostips.com/forum/viewtopic.php?f=3&t=5016):
I converted this file into an .HTML one with TextToHtml.bat program (remember that if you want to do the same thing, the img tag and all its nested url tags must be written in a single long line with no spaces); then, I extracted the part of the .HTML code corresponding to the image subdivision, modified it slightly and used it to complete this program:
The result is pretty good!
These are just a couple examples of the use of this technique. The method may be used in a simpler way and with a wider range of "input form elements" if Batch/JScript/HTA routines are written in order to receive just the minimum essential parameters and then calculate all the values required to show the "input form" in a pleasant standard layout, in a way similar of the "default values + modifiers" provided by my TextToHtml.bat conversion program.
Antonio
PS - A subsequent application that makes good use of this same idea is Drawing HTA "canvas" graphics in Batch files
Then, I saw this request in SO site where the user wants to show something similar to a part of a web page and "get input" from it into a Batch file. I remembered that "HTA files are similar to web pages" and that is possible to write a Batch-HTA hybrid script, so I googled for this topic and found this MS site that explains the details about .HTA files. After some research I wrote the program below that, in my opinion, is the first useful example of a Batch-HTA hybrid script.
Code: Select all
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="closeHTA(1);">First option</button>
<button onclick="closeHTA(2);">Second option</button>
<button onclick="closeHTA(3);">Third option</button>
</BODY>
</HTML>
Code: Select all
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Radio Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(440,170);
var reply = "No button selected";
function closeHTA(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<p>Which prize do you prefer?</p>
<label><input type="radio" name="prize" onclick="reply=this.value" value="House">House</label>
<label><input type="radio" name="prize" onclick="reply=this.value" value="Money">$1 million</label>
<label><input type="radio" name="prize" onclick="reply=this.value" value="None">No prize thanks, I'm already happy <b>:)</b></label>
<br><br>
<button onclick="closeHTA();">Submit</button>
</BODY>
</HTML>
I wrote the text file below in order to subdivide the image in parts using the format allowed by my TextToHtml.bat program (http://www.dostips.com/forum/viewtopic.php?f=3&t=5016):
Code: Select all
=
[img border="0"]Inicio.png
[url="1,1"]rect=0,0,252,124[/url]
[url="1,2"]rect=252,0,508,124[/url]
[url="1,3"]rect=508,0,636,124[/url]
[url="1,4"]rect=636,0,760,124[/url]
[url="2,1"]rect=0,124,252,252[/url]
[url="2,2"]rect=252,124,508,252[/url]
[url="2,3"]rect=508,124,636,252[/url]
[url="2,4"]rect=636,124,760,252[/url]
[url="3,1"]rect=0,252,252,380[/url]
[url="3,2"]rect=252,252,508,380[/url]
[url="3,3"]rect=508,252,760,380[/url]
[url="4,1"]rect=0,380,252,504[/url]
[url="4,2"]rect=252,380,508,504[/url]
[url="4,3"]rect=508,380,760,504[/url]
[/img]
Code: Select all
<!-- :: Batch section
@echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in ("Escritorio Calendario Explorer Tienda"
"Correo Fotos Mapas SkyDrive"
"Contactos Finanzas Deportes"
"Mensajes ElTiempo Noticias") do (
set /A i+=1, j=0
for %%b in (%%~a) do (
set /A j+=1
set "option[!i!,!j!]=%%b"
)
)
cls
echo Get options from an image subdivided in rectangular areas.
:nextOption
echo/
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo Option selected: "!option[%HTAreply%]!"
if "%HTAreply%" equ "4,3" goto endProg
pause
goto nextOption
:endProg
echo End of example
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>Select an option ("Noticias" to end)</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(802,576);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<map name="map2">
<area shape="rect" coords="0,0,252,124" onClick="closeHTA('1,1')">
<area shape="rect" coords="252,0,508,124" onClick="closeHTA('1,2')">
<area shape="rect" coords="508,0,636,124" onClick="closeHTA('1,3')">
<area shape="rect" coords="636,0,760,124" onClick="closeHTA('1,4')">
<area shape="rect" coords="0,124,252,252" onClick="closeHTA('2,1')">
<area shape="rect" coords="252,124,508,252" onClick="closeHTA('2,2')">
<area shape="rect" coords="508,124,636,252" onClick="closeHTA('2,3')">
<area shape="rect" coords="636,124,760,252" onClick="closeHTA('2,4')">
<area shape="rect" coords="0,252,252,380" onClick="closeHTA('3,1')">
<area shape="rect" coords="252,252,508,380" onClick="closeHTA('3,2')">
<area shape="rect" coords="508,252,760,380" onClick="closeHTA('3,3')">
<area shape="rect" coords="0,380,252,504" onClick="closeHTA('4,1')">
<area shape="rect" coords="252,380,508,504" onClick="closeHTA('4,2')">
<area shape="rect" coords="508,380,760,504" onClick="closeHTA('4,3')">
</map>
<img border="0" usemap="#map2" src="Inicio.png" name="img1">
</BODY>
</HTML>
These are just a couple examples of the use of this technique. The method may be used in a simpler way and with a wider range of "input form elements" if Batch/JScript/HTA routines are written in order to receive just the minimum essential parameters and then calculate all the values required to show the "input form" in a pleasant standard layout, in a way similar of the "default values + modifiers" provided by my TextToHtml.bat conversion program.
Antonio
PS - A subsequent application that makes good use of this same idea is Drawing HTA "canvas" graphics in Batch files