Using "HTA input forms" in Batch files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Using "HTA input forms" in Batch files

#1 Post by Aacini » 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.

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>
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:

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>
... 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:

Image

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]
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:

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>
The result is pretty good!

Image

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using "HTA input forms" in Batch files

#2 Post by foxidrive » 29 Jul 2015 22:53

I can't grasp it all Antonio - too little sleep - but I just wanted to say that on my screen the output from this script doesn't seem to be what I would expect by glancing at the script.

Aacini wrote:

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>



Here's my screen shot: is it a font size issue? or Windows 8.1 32 bit issue?

Image

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Using "HTA input forms" in Batch files

#3 Post by Meerkat » 29 Jul 2015 23:43

Aacini wrote:

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>

Great idea! :D

Just to make a smooth "opening" of HTA, put the <HTA:APPLICATION SCROLL="no" SYSMENU="no" > on the bottom of the </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%"
pause
goto :EOF
-->


<HTML>
<HEAD>

<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>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" SHOWINTASKBAR="no" BORDER="thin">
</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>


Also added more 'cool' properties in <HTA:APPLICATION> tag (HTA cannot be resized etc.)

Hope this helps! :roll:

Meerkat

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#4 Post by OperatorGK » 30 Jul 2015 01:47

Guys from one Russian forum done it before (folder selection menu):

Code: Select all

@echo off 
for /f "usebackq delims=" %%i in (
    `@"%systemroot%\system32\mshta.exe" "javascript:var objShellApp = new ActiveXObject('Shell.Application');var Folder = objShellApp.BrowseForFolder(0, 'Select Folder:',1, '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');try {new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch (e){};close();" ^
    1^|more`
) do set sFolderName=%%i
if defined sFolderName (
    echo Folder : %sFolderName%
) else (
    echo Folder isn't selected
)
cd %sFolderName%

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Using "HTA input forms" in Batch files

#5 Post by Meerkat » 30 Jul 2015 03:31

This is quite strange (maybe discovered already):

It tried:

Code: Select all

mshta javascript:alert("HelloWorld!");close();


It displays the alert, but also displays a little flash of HTA window...

But when I tried:

Code: Select all

mshta javascript:alert("HelloWorld!");any_random_word(close());

Code: Select all

mshta javascript:alert("HelloWorld!");rwejrfjirjijfirj(close());

Code: Select all

mshta javascript:alert("HelloWorld!");qqqqqqqqqqqqqqqqqqqq(close());

Code: Select all

mshta javascript:alert("HelloWorld!");blablablablablallbala(close());


It displays the alert, but does not display the little flash. :roll:

Edit

For VBScript,

Code: Select all

mshta vbscript:Execute("msgbox (""Hello World!""):fjidjsijdsjiiiubhfui close")

Code: Select all

mshta vbscript:Execute("msgbox (""Hello World!""):do_NOT_put_number_here close")


Meerkat

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#6 Post by OperatorGK » 30 Jul 2015 08:09

Meerkat wrote:This is quite strange (maybe discovered already):

It tried:

Code: Select all

mshta javascript:alert("HelloWorld!");close();


It displays the alert, but also displays a little flash of HTA window...

But when I tried:

Code: Select all

mshta javascript:alert("HelloWorld!");any_random_word(close());

Code: Select all

mshta javascript:alert("HelloWorld!");rwejrfjirjijfirj(close());

Code: Select all

mshta javascript:alert("HelloWorld!");qqqqqqqqqqqqqqqqqqqq(close());

Code: Select all

mshta javascript:alert("HelloWorld!");blablablablablallbala(close());


It displays the alert, but does not display the little flash. :roll:

Edit

For VBScript,

Code: Select all

mshta vbscript:Execute("msgbox (""Hello World!""):fjidjsijdsjiiiubhfui close")

Code: Select all

mshta vbscript:Execute("msgbox (""Hello World!""):do_NOT_put_number_here close")


Meerkat

It's intented behavior:
HTA window pops out when all javascript is executed and there is no code in buffer. But close() function has a little delay (0,1 ms maybe?) between execution stopping and window closing. So HTA window appears immediately after close() is executed, and after 0,1 ms is closed. But if we put close() in another function (ever non-existent), this function name is loaded into memory. After close() starts there is still some code in buffer, so HTA window don't even tries to load. But this code is never executed cause close() function stops code execution, and after 0,1 ms process is finally killed.
EDIT: Now easier to read.
Last edited by OperatorGK on 30 Jul 2015 08:14, edited 1 time in total.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using "HTA input forms" in Batch files

#7 Post by Aacini » 30 Jul 2015 08:10

foxidrive wrote:I can't grasp it all Antonio - too little sleep - but I just wanted to say that on my screen the output from this script doesn't seem to be what I would expect by glancing at the script...

... is it a font size issue? or Windows 8.1 32 bit issue?



It is just matter of local font size/screen resolution/etc combination, foxi; just modify the values in this line:

Code: Select all

window.resizeTo(374,100);

until it looks right for you.

As I said at end of my post, the need of manual adjustments can be avoided via additional code that calculate all required values based on local settings in order to assemble a pleasant visual presentation layout...

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using "HTA input forms" in Batch files

#8 Post by foxidrive » 30 Jul 2015 08:22

Aacini wrote:It is just matter of local font size/screen resolution/etc combination, foxi; just modify the values in this line:

Code: Select all

window.resizeTo(374,100);

until it looks right for you.

As I said at end of my post, the need of manual adjustments can be avoided via additional code that calculate all required values based on local settings in order to assemble a pleasant visual presentation layout...

Antonio


I see, thanks.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Using "HTA input forms" in Batch files

#9 Post by Meerkat » 30 Jul 2015 08:24

OperatorGK wrote:
Meerkat wrote:This is quite strange (maybe discovered already):

*snip

Meerkat

It's intented behavior:
HTA window pops out when all javascript is executed and there is no code in buffer. But close() function has a little delay (0,1 ms maybe?) between execution stopping and window closing. So HTA window appears immediately after close() is executed, and after 0,1 ms is closed. But if we put close() in another function (ever non-existent), this function name is loaded into memory. After close() starts there is still some code in buffer, so HTA window don't even tries to load. But this code is never executed cause close() function stops code execution, and after 0,1 ms process is finally killed.
EDIT: Now easier to read.


@OperatorGK Thanks for the explanation! :D

I now use "code" as my function for closing mshta...

Meerkat
Last edited by Meerkat on 30 Jul 2015 08:33, edited 1 time in total.

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#10 Post by OperatorGK » 30 Jul 2015 08:26

Aacini wrote:
foxidrive wrote:I can't grasp it all Antonio - too little sleep - but I just wanted to say that on my screen the output from this script doesn't seem to be what I would expect by glancing at the script...

... is it a font size issue? or Windows 8.1 32 bit issue?



It is just matter of local font size/screen resolution/etc combination, foxi; just modify the values in this line:

Code: Select all

window.resizeTo(374,100);

until it looks right for you.

As I said at end of my post, the need of manual adjustments can be avoided via additional code that calculate all required values based on local settings in order to assemble a pleasant visual presentation layout...

Antonio

Ehm... we can just arrange buttons like so (part of HTML code):

Code: Select all

<p style="text-align: center; font-family: Arial; font-size: small">
  <button> Button A </button>
  <button> Button B </button>
</p>

It's HTML after all guys, there is simple ways to do it. We need to change font size, NOT window size.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using "HTA input forms" in Batch files

#11 Post by Aacini » 08 Aug 2015 08:19

OperatorGK wrote:[...]

It's HTML after all guys, there is simple ways to do it. We need to change font size, NOT window size.

It is HTML of course, so a very long list of adjstments can be done on it. However, the goal is that any Batch file programmer may make good use of this feature with no need to know a single word about HTML, and the way to achieve that is via parameters that the Batch code translate into the appropriate HTML values.

This is the first version of "HTA-Input Form" subroutine that allows any Batch file programer to easily use the HTA window:

Code: Select all

<!-- :: Batch section
@echo off
setlocal

goto begin


Example of "HTA Input Forms" in Batch files
Antonio Perez Ayala, Aug/08/2015

There are 4 "HTA-Input Form" routines implemented in this program:
Button, RadioButton, CheckBox and Password; all of them use the same format:

   call :HTA-Input form result= [/V]  col row width height  option1 option2 ...

"form" is one of the 4 routine names listed above.

If the /V switch is included, the options will be listed vertically.

The next 4 values specify the position and size of the input form in the screen
in text (column and row) coordinates. This feature depends on get the position
and font size of the command-line window in pixels, but I am still working on a
method to get such values without using a third party .exe program. A temporary
solution to this problem is to create a copy of the command-line icon, right-
click on it, select "Properties" > "Layout", unset "Let the system locate the
window" and set a fixed screen position, like 190,10; also, get the size of the
font from "Font" tab. Then, hardcode these four values into this program (look
for the "PATCH" related comments) and use the modified icon to run it.

Each "option" is a word or a series of words enclosed in quotes.

When the form is RadioButton, the pre-checked option may be indicated preceding
it by /C: switch; in this case the quotes are mandatory. For example:

   call :HTA-Input RadioButton result= col row width height
                           "First opt" /C:"Second opt checked" "Third opt"

If the form is Password, the first option may be a number that specify the
maximum number of characters to read.

When the subroutine ends, "result" variable will have the input value from the
form, or be undefined if the input form was cancelled.

Reference:
http://www.quackit.com/html/tags/html_input_tag.cfm


:begin
cls
echo/
echo ========  EXAMPLE  OF  HTA  INPUT  FORMS  ========

echo/
echo/
echo/
set /P "=HTA-Input Button in vertical menu: " < NUL
call :HTA-Input Button result= /V 36 4 15 8 " First option      " "Second option" " Third option    "
echo Option selected = %result%

echo/
echo/
echo/
set /P "=HTA-Input Button: " < NUL
call :HTA-Input Button result= 19 8 36 5 "First option" "Second option" "Third option"
echo Option selected = %result%

echo/
echo/
echo/
set /P "=HTA-Input RadioButton in vertical menu: " < NUL
call :HTA-Input RadioButton result= /V 41 12 18 10 "First option" /C:"Second option" "Third option"
echo Option selected = %result%

echo/
echo/
echo/
set /P "=HTA-Input RadioButton: " < NUL
call :HTA-Input RadioButton result= 24 16 46 7 "First option" "Second option" /C:"Third option"
echo Option selected = %result%

echo/
echo/
echo/
set /P "=HTA-Input CheckBox in vertical menu: " < NUL
call :HTA-Input CheckBox result= /V 38 20 18 10 "First option" "Second option" "Third option"
echo Option(s) selected = "%result%"

echo/
echo/
echo/
set /P "=HTA-Input CheckBox: " < NUL
call :HTA-Input CheckBox result= 21 24 46 7 "First option" "Second option" "Third option"
echo Option(s) selected = "%result%"

echo/
echo/
echo/
set /P "=HTA-Input Password: " < NUL
call :HTA-Input Password result= 21 28 17 8 8
echo Password read = "%result%"
goto :EOF


==================================================================================
This is :HTA-Input Form subroutine; include from this point until EOF in your code
Don't forget to also copy *the first line* in this file!

Developed by Antonio Perez Ayala aka Aacini
http://www.dostips.com/forum/viewtopic.php?f=3&t=6581&p=42362#p42362


:HTA-Input form result= [/V]  col row width height  option1 option2 ...
setlocal EnableDelayedExpansion
set "form=%1" & shift
set "res=%1" & shift
if /I "%~1" equ "/V" (set "ver=<br>" & shift) else set "ver="
set "pos=%1 %2 %3 %4"
for /L %%i in (1,1,4) do shift
echo %form% > HTML
set i=0
goto %form%

:Button
   set /A i+=1
   set /P "=<input type="button" onclick="closeHTA(%i%)" value="%~1">%ver% " >> HTML < NUL
   shift
if "%~1" neq "" goto Button
call :GetHTAreply
endlocal & set "%res%=%HTAreply%"
exit /B

:RadioButton
   set "button=%~1"
   if not defined button goto endButton
   set /A i+=1
   set "checked="
   if /I "!button:~0,3!" equ "/C:" (
      set "button=!button:~4,-1!"
      set "checked=checked"
      set "pos=%pos% %i%"
   )
   set /P "=<label><input type="radio" name="RB" onclick="rb=%i%" value="%i%" %checked%>%button%</label>%ver% " >> HTML < NUL
   shift
goto RadioButton
:endButton
set /P "=<br><br><button onclick="closeHTA(rb);">Submit</button>" >> HTML < NUL
call :GetHTAreply
endlocal & set "%res%=%HTAreply%"
exit /B

:CheckBox
   set /A i+=1
   set /P "=<input type="checkbox" name="CB" onclick="checkBox('%i%')" value="%i%">%~1&nbsp;%ver%" >> HTML < NUL
   shift
if "%~1" neq "" goto CheckBox
set /P "=<br><br><button onclick="closeHTA(cb);">Submit</button>" >> HTML < NUL
call :GetHTAreply
endlocal & set "%res%=%HTAreply%"
exit /B

:Password
if "%~1" neq "" (set maxlength=maxlength="%~1") else set "maxlength="
set /P "=<input type="password" id="PW" %maxlength%>" >> HTML < NUL
set /P "=<br><br><button onclick="closeHTA(document.getElementById('PW').value)">Submit</button>" >> HTML < NUL
call :GetHTAreply
endlocal & set "%res%=%HTAreply%"
exit /B


:GetHTAreply
set "HTAreply="
for /F "delims=" %%a in ('(echo %pos% ^& type HTML ^) ^| mshta.exe "%~F0"') do set "HTAreply=%%a"
del HTML
exit /B

-->

<HTML>

<HEAD>
<HTA:APPLICATION INNERBORDER="no" SYSMENU="no" SCROLL="no" >
   <style type="text/css">
   body {
      color: white;
      background: black;
      font-family: "Lucida Console", monospace;
   }
   </style>
</HEAD>

<BODY>
</BODY>

<SCRIPT language="JavaScript">

var fso     = new ActiveXObject("Scripting.FileSystemObject"),
    stdin   = fso.GetStandardStream(0),
    pos     = stdin.ReadLine().split(" "), rb = pos[4], cb = " ",
    cmdExe  = window.parent,

    // PATCH: Uncomment next lines when "window.parent" property correctly works in your browser
    // winLeft = cmdExe.screenLeft ? cmdExe.screenLeft : cmdExe.screenX,
    // winTop  = cmdExe.screenTop  ? cmdExe.screenTop  : cmdExe.screenY,
    // else, place here the fixed screen position you set for your cmd.exe window
    winLeft = 190, winTop = 10,

    // PATCH: Uncomment next lines *in IE only* when "window.parent" correctly works ("currentStyle" property is IE exclusive)
    // fontY   = parseInt(cmdExe.body.currentStyle.fontSize),
    // fontX   = Math.round(fontY*6/10);
    // else, place here the size of font used in cmd.exe window
    fontX = 11, fontY = 18;

window.moveTo(winLeft+fontX*pos[0],winTop+fontY*pos[1]);
window.resizeTo(fontX*pos[2],fontY*pos[3]);
document.title = "HTA Input "+stdin.ReadLine();
document.body.innerHTML = stdin.ReadLine();

function checkBox(opt){
   if ( cb.indexOf(" "+opt+" ") >= 0 ) {
      cb = cb.replace(" "+opt+" "," ");
   } else {
      cb += opt+" ";
   }
}

function closeHTA(reply){
   fso.GetStandardStream(1).WriteLine(reply);
   window.close();
}

</SCRIPT>

</HTML>

Below there is a collage comprised of several screen segments that show how the screen looks before and after the execution of the ":HTA-Input Form" subroutine with each one of the different input forms:

Image

Important: If you copy previous code and run it on your computer, it is unlikely that you get the same output above, unless the cmd.exe command-line window use a 11x18 size font and be located at position 190,10 on the screen. A very simple adjustment can be done in order to get the appropriate otput; just follow the indications given in the description section of the program.

I tried to get the font size and position of the cmd.exe window via "window.parent" property that (accordingly to specifications) should give access to it, but with no success; I think that the cause of this problem is that the "parent window" is precisely a command-line cmd.exe window, that behaves slightly different than standard windows. It seems that the only way to get this information is via an auxiliary .exe program (I'll appreciate any advice on this point from a HTML expert).

When I was developing this program I found a very strange bug: the execution of the HTA window fail in certain cases depending on the context in which the mshta.exe program is executed! For example, if I change the "call :GetHTAreply" lines by "goto :GetHTAreply" and move the "endlocal & ..." line at end of :GetHTAreply, then the HTA window is open with no change in its position/size/contents: just a large empty window!!! I have no idea of the cause of this bug...

Antonio

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#12 Post by OperatorGK » 08 Aug 2015 12:39

AFAIK, window.parent works only in browser engine environment, such as MS HTA windows and IFRAMEs.
Also, I'll do some research on the bug.

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#13 Post by OperatorGK » 08 Aug 2015 13:19

All is OK with MSHTA, it's a bug in your script:
Call clears arguments, but goto doesn't. When using call, %~F0 is reset to file path, but if we use goto, it will be "Third option". As there is no such file, blank window will be displayed. Also, I didn't know that shift changes %0.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using "HTA input forms" in Batch files

#14 Post by Aacini » 11 Aug 2015 09:43

OperatorGK wrote:All is OK with MSHTA, it's a bug in your script:
Call clears arguments, but goto doesn't. When using call, %~F0 is reset to file path, but if we use goto, it will be "Third option". As there is no such file, blank window will be displayed. Also, I didn't know that shift changes %0.

You are right, this was the cause of the error! I solved it defining "myself=%~F0" in :HTA-Input subroutine and using it later instead of "%~F0".

I tried to use document.defaultView/document.parentWindow instead of window.parent, but don't works either. Is there any way to get the coordinates of the cmd.exe window from the HTA?

OperatorGK wrote:It's intented behavior:
HTA window pops out when all javascript is executed and there is no code in buffer. ...

Do you know of any site that describe this behaviour? TIA

Antonio

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: Using "HTA input forms" in Batch files

#15 Post by OperatorGK » 11 Aug 2015 14:00

Aacini wrote:
OperatorGK wrote:All is OK with MSHTA, it's a bug in your script:
Call clears arguments, but goto doesn't. When using call, %~F0 is reset to file path, but if we use goto, it will be "Third option". As there is no such file, blank window will be displayed. Also, I didn't know that shift changes %0.

You are right, this was the cause of the error! I solved it defining "myself=%~F0" in :HTA-Input subroutine and using it later instead of "%~F0".

I tried to use document.defaultView/document.parentWindow instead of window.parent, but don't works either. Is there any way to get the coordinates of the cmd.exe window from the HTA?

OperatorGK wrote:It's intented behavior:
HTA window pops out when all javascript is executed and there is no code in buffer. ...

Do you know of any site that describe this behaviour? TIA

Antonio

It isn't a site, it's how I figured it out. I put together my observations and most obvious way to code this. It's much like how we describe cmd.exe parsing rules - we don't really know how it is coded, but we know how it appears to be. And I don't know exactly how to retrieve coordinates, but it certainly isn't through window or document object, but it goes from object such as Shell.Application and others: just google for full list of WScript objects, you can use them in HTA except WScript.

Post Reply