I'm continuing my games with self contained source files.Now I want to see how can I pass arguments to mshta - mainly to reduce mshta call line. The obvious way is to open stdin - ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0)
here's an .bat/gui file that creates radio button for each passed argument and at the end returns the number of the choosen button:
Code: Select all
@if (@X)==(@Y) @end /*JScript comment
@echo off
(
echo %*
)|(
mshta "about:<hta:application ShowInTaskbar=No Caption=no><title>radio</title><body onload='prepare()'><script language='javascript' src='file://%~dpnxf0'></script><span id='container'>All we hear is: <br></span></body>"
)|(
for /f "tokens=* delims=" %%R in ('more') do (
(echo(%%R)
)
)
rem echo %result%
exit /b 0
**/
var argline;
var result;
var errmessage;
var elements = new Array();
var textnode;
function clckd(){
// alert(a);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
//fso.Write(line);
//close(fso.Write(a));
for (i = 0; i < elements.length; i++) {
if (elements[i].checked ) {
close(fso.Write(i+1));
//alert(i+1);
}
}
}
function prepare(){
try {
var fso2= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
argline=fso2.ReadLine();
//alert(argline);
//argline = "radio gaga radio gogo someone still loves you"
var args=argline.split(" ");
} catch (err) {
errmessage = "cannot get the input";
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
fso.Write(errmessage);
}
var br_element = document.createElement('br');;
var i;
var temp_element;
for (i = 0; i < args.length; i++) {
temp_element = document.createElement("input");
temp_element.setAttribute("type", "radio");
temp_element.setAttribute("name", "gaga");
temp_element.setAttribute("value", i);
temp_element.setAttribute("id", "gaga".concat(i));
temp_element.value=args[i];
elements.push(temp_element);
}
for (i = 0; i < args.length; i++) {
var br_element = document.createElement('br');;
var container = document.getElementById('container');
textnode=document.createTextNode("_"+args[i]+".");
container.appendChild(elements[i]);
container.appendChild(textnode);
container.appendChild(br_element);
}
container.appendChild(br_element);
var submit = document.createElement("input");
submit.setAttribute("type", "button");
submit.setAttribute("value", "submit");
submit.onclick= function() {clckd();};
container.appendChild(submit);
//alert(document.getElementById('container').innerHTML);
}
the result (I still have one unwanted element):
According to this post:
Code: Select all
http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/20/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts.aspx
the hta has HTA.ID.commandLine
and it works with javascript too (though it show the also the call of mshta also):
Code: Select all
@if (@X)==(@Y) @end /*JScript comment
@echo off
mshta "about:<hta:application ID='objTestHTA' ShowInTaskbar=yes Caption=no><title>radio</title><body onload='Window_Onload()'><script language='javascript' src='file://%~dpnxf0'></script><span id='container'><br></span></body>" test arguments|more
rem echo %result%
exit /b 0
**/
function Window_Onload(){
alert(objTestHTA.commandLine);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1)
for (i in objTestHTA) {
fso.writeline(i);
}
fso.writeline("---parent element---");
for (i in objTestHTA.parentElement) {
fso.writeline(i);
}
fso.writeline("---body---");
for (i in document.body) {
fso.writeline(i);
}
alert(objTestHTA.parentElement.parentElement.ownerDocument)
close();
}
The reason I print the document and hta object properties is to find
Window_onLoad function but I think its not accessible through javascript (javascript is case sensitive while vbscript is not - I've tried few variants but may be I should test all of them .But in vbscript window_onload is defined as subroutine , but function.Javascript has only functions ...).
With vbscript it works perfectly.
Code: Select all
:sub echo(str) :end sub
echo off
'>nul 2>&1|| copy /Y %windir%\System32\doskey.exe '.exe >nul
'& echo/
'& echo %~nx0
'& echo asd |mshta "about:<html><head><title>Command Line Agruments</title><HTA:APPLICATION ID='objTestHTA' APPLICATIONNAME='Command Line Agruments' SINGLEINSTANCE='yes'></head><SCRIPT Language='VBScript' src='%~dpnxf0'></SCRIPT><body></body></html>" asd dsa asd
'& exit /b
Sub Window_onLoad
Msgbox objTestHTA.commandLine
arrCommands = Split(objTestHTA.commandLine, chr(34))
For i = 3 to (Ubound(arrCommands) - 1) Step 2
Msgbox arrCommands(i)
Next
close
document.wite(objTestHTA.commandLine)
'Msgbox objTestHTA.version
End Sub
'http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/20/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts.aspx
some things that I've used
http: //scripting.cocolog-nifty.com/blog/2006/09/mshtaexeurl_40fe.html - a japanese guy that posted info how mshta can be called from command line
https: //groups.google.com/forum/#!msg/alt.msdos.batch.nt/b8q29_uLfQs/4uoVLRo1uOsJ - Tom Lavedas technique for bat/jscript files
http: //with-love-from-siberia.blogspot.com/2012/10/continuing-long-lines-in-batch-script.html - siberia-man
http: //ss64.org/viewtopic.php?pid=6195#p6195 here saw jeb to pipe to for /f
http: //blogs.technet.com/b/heyscriptingguy/archive/2005/04/20/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts.aspx - technet about hta command line arguments.
http: //stackoverflow.com/a/16622325/388389 - how to create bat/vbs hybrid