js/vbs/html/hta and more hybrids and chimeras in cmd/bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#46 Post by npocmaka_ » 15 Jun 2015 05:58

you need to call the job by its ID:


Code: Select all

@cScript.EXE //noLogo "%~f0?.WSF" //job:javaScript /arg0:sleep_ /arg1:time
@cScript.EXE //noLogo "%~f0?.WSF" //job:VBScript /arg0:sleep_ /arg1:time
@exit /b 0


<package>
   <job id="javaScript">
      <script language="JScript">
      WScript.Echo( "hi" );
                 WScript.Echo(WScript.Arguments.Item(1));
      </script>
   </job>

  <job id="VBScript">
   <script language="VBScript">
      WScript.Echo("blah"):
   </script>
  </job>
</package>


and you need to put your jobs in a package.

Ed Dyreen
Expert
Posts: 1441
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#47 Post by Ed Dyreen » 15 Jun 2015 06:16

My god what is wrong with me today, I even misspelled job ID. :roll:

thank u very much npocmaka_, it works now :D

npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#48 Post by npocmaka_ » 25 Jun 2015 05:24

mshta without splash :

Code: Select all

@echo off
mshta.exe "%~f0"|more
exit /b

<HTA:Application
       ShowInTaskbar = no
   WindowsState=Minimize
   SysMenu=No
   ShowInTaskbar=No
   Caption=No
   Border=Thin
>

<script language="javascript" type="text/javascript">
   window.visible=false;
   window.resizeTo(1,1);
   
   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
   fso.WriteLine("waiting 5 seconds");
     function waitSeconds(iMilliSeconds) {
      var counter= 0
         , start = new Date().getTime()
         , end = 0;
      while (counter < iMilliSeconds) {
         end = new Date().getTime();
         counter = end - start;
      }
     }
     waitSeconds(5000);

  var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
  close(fso.Write("test hta window visibility"));
</script>


the mshta splash is not visible , but for moment the focus is lost from the cmd console.
At the beginning it sets some hta tags to reduce interaction elements (though the hta part can be removed - it is not the important part - without it for a moment a small window will be visible while application is closed) :

Code: Select all

https://technet.microsoft.com/en-us/library/ee176567.aspx


then applies on the window object (the mother and the father of everything in javascript when the context is a browser) resize and visibility to false.
as the HTA is some kind of internet explorer you can use methods/properties from IWebBrowser2 :

Code: Select all

https://msdn.microsoft.com/en-us/library/aa752127(v=vs.85).aspx

npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#49 Post by npocmaka_ » 01 Jul 2015 02:47

One more reason to love mshta.With hta application you can use ecmascript 6 features (while wsh jscript is ecmascript 4 compatible ):


Code: Select all

@echo off
mshta.exe "%~f0"|more
exit /b

<HTA:Application
   ShowInTaskbar = no
   WindowsState=Minimize
   SysMenu=No
   ShowInTaskbar=No
   Caption=No
   Border=Thin
>
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<script language="javascript" type="text/javascript">
    window.visible=false;
    window.resizeTo(1,1);
   var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
   var obj = JSON.parse(text);
   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
   fso.Write(obj.name + "\r\n" + obj.street + "\r\n" + obj.phone);
   close();
</script>


You need installed IE11 and <meta http-equiv="x-ua-compatible" content="ie=edge" /> . Probably will be not so useful for old XP/2003 machines , but is good to know as allows usage of callback functions , promises , JSON and so on. Including some cool javascript libraries that cannot be used with wsh/jscript.

siberia-man
Posts: 91
Joined: 26 Dec 2013 09:28
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#50 Post by siberia-man » 21 Dec 2015 05:52

The script was updated: http://www.dostips.com/forum/viewtopic.php?f=3&t=5543&p=37780#p37780

Some cosmetic changes and support for Python were done.

siberia-man
Posts: 91
Joined: 26 Dec 2013 09:28
Contact:

Re: js/vbs/html/hta hybrids and chimeras in cmd/bat

#51 Post by siberia-man » 21 Jul 2016 09:27

The script was updated: http://www.dostips.com/forum/viewtopic.php?f=3&t=5543&p=37780#p37780.
After this thread We can use bash in our scripts now in Win10 I decided to add (the variant with heredoc) the support of hybrid of Bash in Cmd.

npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta and more hybrids and chimeras in cmd/bat

#52 Post by npocmaka_ » 21 Jul 2016 12:15

Anyway the thread was resurrected...
Here's one more highly imperfect way to make a hybrid with vbscript that just came to me:

Code: Select all

:sub c() :end sub:
C: '& @echo off
C: '& echo batch
C: '& cscript /nologo /E:vbscript %~f0
C: '& exit /b 0

WScript.Echo "VBScript"
WScript.Quit


it just uses jump to drives in the batch part and for the drive letter there's a prepared subroutine in the vbscript part.

npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta and more hybrids and chimeras in cmd/bat

#53 Post by npocmaka_ » 16 Sep 2016 13:27

turns out rundll32 also can execute jsvascript/vbscript code (though it is calling the mshtml.dll):

rundll32.exe javascript:"\..\mshtml.dll,RunHTMLApplication ";eval("w=new%20ActiveXObject(\"WScript.Shell\");w.Exec(\"calc\")");


https://gist.github.com/subTee/62fc28bb ... 6d65921bd2

npocmaka_
Posts: 446
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: js/vbs/html/hta and more hybrids and chimeras in cmd/bat

#54 Post by npocmaka_ » 07 Feb 2017 07:41

Rather an important note for the wsh scripts.

To be able to pass non named arguments to the script that starts with '//' you'll need to put '//' after the script name:

Code: Select all

@if (@X)==(@Y) @end /* JScript comment
    @echo off
    cscript //E:JScript //nologo "%~f0" // %*
    exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */

WScript.Echo(WScript.Arguments.Item(0));

Post Reply