Search code examples
user-interfacebatch-filecmdhta

Input form for batch with HTA


How can I make a simple input form in HTA and read it Batch in a simple variable, and a button to proceed. I don't know any other way to make a decent GUI for a batch file. Something like this

enter image description here

Any help is appreciated.


Solution

  • In short, all you need to do is

    • Get a reference to the standard output stream inside the .hta file and write to this output the content of the field

    • Execute the .hta from batch file using a for /f command to process the data sent to standard output.

    The only "problem" is that you need to explicitly call the mshta.exe executable with the full path to the .hta file.

    serialNumber.hta

    <HTML>
        <HEAD>
            <HTA:APPLICATION 
                ID              = "serialHTA" 
                APPLICATIONNAME = "serialHTA"
                VERSION         = "0.1"
                NAVIGABLE       = "yes"
                SHOWINTASKBAR   = "yes" 
                SINGLEINSTANCE  = "yes" 
                WINDOWSTATE     = "normal"
    
                BORDER          = "normal" 
                BORDERSTYLE     = "normal"
                INNERBORDER     = "no" 
    
                CAPTION         = "yes" 
                MINIMIZEBUTTON  = "yes"
                MAXIMIZEBUTTON  = "yes"
                SYSMENU         = "yes" 
                SCROLL          = "yes" 
                SCROLLFLAT      = "yes"
    
                CONTEXTMENU     = "yes"
                SELECTION       = "yes"
            />
    
            <TITLE>input serial number</TITLE>
    
            <SCRIPT language="Javascript">
                function closeHTA(sendOutput){
                    if (sendOutput){
                        (new ActiveXObject('Scripting.FileSystemObject'))
                            .GetStandardStream(1)
                            .WriteLine(
                                document.getElementById('serialNumber').value
                            );
                    };
                    window.close();
                }
            </SCRIPT>
    
        </HEAD>
    
        <BODY>
            <label for="serialNumber">serial:</label>
            <input type="text" id="serialNumber">
            <br>
            <button onclick="closeHTA(true);">ok</button>
            <button onclick="closeHTA();">cancel</button>
        </BODY>
    
    </HTML>
    

    serialNumber.cmd

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        set "serialNumber="
        for /f "delims=" %%a in ('mshta.exe "%~dp0\serialnumber.hta"') do set "serialNumber=%%a"
    
        if defined serialNumber (
            echo Serial number: %serialNumber%
        ) else (
            echo No serial number provided
        )