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: 512
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: 1569
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: 512
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: 512
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: 208
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: 208
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: 512
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: 512
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: 512
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));

Marblo
Posts: 1
Joined: 05 Apr 2018 15:25

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

#55 Post by Marblo » 05 Apr 2018 15:40

Maybe you could even add ps support to this script. I don't know how to invoke the batch as a .ps script but the output could look like this :

Code: Select all

 
<# :: batch part
Echo hello world
Exit
#>
(some ps commands) 


Sorry for typos or bad english :rol: :wink:

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#56 Post by Squashman » 05 Apr 2018 19:52

Marblo wrote:
05 Apr 2018 15:40
Maybe you could even add ps support to this script. I don't know how to invoke the batch as a .ps script but the output could look like this :
Read the whole thread.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#57 Post by ShadowThief » 06 Apr 2018 06:11

Marblo wrote:
05 Apr 2018 15:40
Maybe you could even add ps support to this script. I don't know how to invoke the batch as a .ps script but the output could look like this :

Code: Select all

 
<# :: batch part
Echo hello world
Exit
#>
(some ps commands) 


Sorry for typos or bad english :rol: :wink:
PowerShell support already exists for this script. It's the one time I've ever used it.

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#58 Post by jfl » 01 May 2018 11:49

I think the PowerShell support can be simplified, based on a post at https://stackoverflow.com/a/49063524

Here's an improved version of their script:

Code: Select all

<# :# PowerShell comment protecting the Batch section
@echo off
setlocal EnableExtensions DisableDelayedExpansion

:# Prepare batch arguments
set ARGS=%*
if defined ARGS set ARGS=%ARGS:"=\"%
if defined ARGS set ARGS=%ARGS:'=''%

echo In Batch
PowerShell -c ^"Invoke-Expression ('^& { ' + [io.file]::ReadAllText(\"%~f0\") + '} %ARGS%')"
echo Back in Batch. PowerShell exit code = %ERRORLEVEL%
exit /b

###############################################################################
# End of the PS comment around the Batch section; Begin the PowerShell section #>

echo "In PowerShell"
$Args | % { "PowerShell Args[{0}] = '$_'" -f $i++ }
exit 0
The important trick I added is to ^escape the first double quote in the PowerShell command.
This ensures that the ^ & | < > special characters in quoted batch arguments are still protected by their quotes in the PowerShell command line, and make it through safely.
Cherry on the cake, you can even pass double quotes by doubling them:

Code: Select all

C:\JFL\SRC\Batch>"bat+ps1.bat" one 2 "three four $#^!&|<>'"""
In Batch
In PowerShell
PowerShell Args[0] = 'one'
PowerShell Args[1] = '2'
PowerShell Args[2] = 'three four $#^!&|<>'"'
Back in Batch. PowerShell exit code = 0

C:\JFL\SRC\Batch>

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

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

#59 Post by siberia-man » 05 May 2018 04:12

jfl wrote:
01 May 2018 11:49
I think the PowerShell support can be simplified, based on a post at https://stackoverflow.com/a/49063524
I used another answer from that thread - https://stackoverflow.com/a/2611487/3627676

DQ2000
Posts: 38
Joined: 07 Aug 2019 17:26

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

#60 Post by DQ2000 » 06 Nov 2019 16:14

Great!! with this aces the double buffer and graphics :lol: :lol: :lol: :lol: :lol: :lol: :lol:

Post Reply