mshta usage

mshta usagemshtaisshortforMicroSoftHtmlApplication.Itcouldrunhtmlfileorhtmlstringasaparameter.whatisinterestingisyoucanuseitinawindowbatchcommandorbatchfileandyoucanusesome

大家好,又见面了,我是你们的朋友全栈君。

  • mshta is short for MicroSoft Html Application. It could run html file or html string as a parameter.
  • what is interesting is you can use it in a batch command or batch file and you can use some functions like in the browser such as show a dialog and so on.
  • with mshta you can run javascript code and vbscipt code in windows command line or batch file.

Example 1, show a message box in many different ways.

mshta javascript:window.execScript("msgBox('hello world!'):window.close","vbs") 
mshta vbscript:window.execScript("alert('hello world!');close()","javascript") 
mshta vbscript:window.execScript("msgBox('hello world!'):window.close","vbs")
mshta javascript:alert("hello world!");close()
mshta vbscript:msgbox("hello world!",64,"Title")(window.close)
mshta vbscript:CreateObject("Wscript.Shell").popup("hello world!",7,"Title",64)(window.close) 

Example 2, execute more than one commands in one line.(In fact this already shows in Example 1, you must find it, it is the close command.)

mshta vbscript:execute("msgbox ""BOX one"":msgbox ""BOX two"":window.close") 
mshta vbscript:(msgbox("BOX one"))(msgbox("BOX two")(window.close))
mshta javascript:alert("BOX one",);alert("BOX two");close()
mshta javascript:execScript("msgBox('BOX one'):msgBox('BOX two'):window.close","vbs")

Example 3, use ActiveXObject in script to use more sophisticated functions of the OS.

mshta vbscript:createobject("sapi.spvoice").speak("Hello, I am tom, let's do something fun.")(window.close) mshta "javascript:close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('Hello! I am '+V(0).GetAttribute('Gender')))"

Example 4, javascript is much more easy to use, because you do not have to add so many double qoutes like in vbscript.

mshta "javascript:var objFSO=new ActiveXObject('Scripting.FileSystemObject'); var objFile = objFSO.CreateTextFile('test.txt',true); objFile.Write('Hello World.');objFile.Close();close();" mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'hello world!', 10, 'Title!', 64 );close()"

Example 5, several ways to calculate the free memory of your computer.

mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes));"|more
for  /f "usebackq" %a in (`mshta ^"javascript^:close^(new ActiveXObject^(^'Scripting.FileSystemObject^'^).GetStandardStream^(1^).Write^(GetObject^(^'winmgmts:^'^).ExecQuery^(^'Select * from Win32_PerfFormattedData_PerfOS_Memory^'^).ItemIndex^(0^).AvailableBytes^)^);^"^|more`) do set free_mem=%a
mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableMBytes));"|for /f %%a in ('more') do set free_mem=%%a

Example 6, another way to calculate free memory.

@echo off
setlocal
:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(" set "endJS=));""
:: Direct instantiation requires that output is piped
%beginJS% GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes %endJS% | findstr "^"
:: FOR /F does not need pipe
for /f %%N in ( '%beginJS% GetObject('winmgmts:').ExecQuery('Select * from Win32_PerfFormattedData_PerfOS_Memory').ItemIndex(0).AvailableBytes %endJS%' ) do set free_mem=%%N echo free_mem=%free_mem%

Example 7, show color pallets dialog.

mshta "about:<script>function b(){ 
    new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(d.ChooseColorDlg().toString(16));close();}</script><body onload='b()'><object id='d' classid='clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b'></object></body>"|more 

Example 8, show screen resolution.

mshta "javascript:res=screen.width+'x'+screen.height;alert(res);close();" 1 | more

Example 9, use clipboard data.

for /f "usebackq tokens=1,* delims=[]" %i in (`mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text')));"^|find /v /n ""`) do @set "c[%i]=%j" 

Example 10, use javascript setTimeout to delay some time interval to execute a command.

mshta javascript:setTimeout('close()',10000)

Example 11, show File Open Select Dialog, you can redirect your output to the command window or to a file or to a variable.

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>"|more

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>">test.txt

mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>">temp && set /p a=<temp for /f "delims=" %%i in ('mshta "about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();</script>"') do set a=%%i

Example 12, show Open For Folder Dialog to select a directory, and you can assign it to a variable.

for /f "delims=" %%i in ('mshta "javascript:var folder=new ActiveXObject("Shell.Application").BrowseForFolder(0,'选择要处理的文件夹', 0, '').self.path;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(folder);close();"') do set input=%%i

Example 13, execute batch command with parameters which has spaces.

::a.bat
@echo off
set text=Hello World
mshta vbscript:createobject("WScript.Shell").Run("b.bat "+"""%text%""",0)(window.close)
::b.bat
@echo off
mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( '%~1', 10, 'Title!', 64 ); close()"

Example 14, ultimate function, run a html file.

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

reference1
reference2
reference3

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159687.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MySql必知必会实战练习(六)游标

    游标主要用于交互式应用,滚动屏幕上的数据,并对数据进行浏览或做出更改看一下下面的例子:输出:由于结果为多行无法显示,这时我们就需要使用游标来操作1.游标基本方法(1)创建游标(2)打开游

    2021年12月29日
    36
  • redisclient命令_redisconnection

    redisclient命令_redisconnectionRedisClientRedis服务器是典型的一对多服务器程序一个服务器可以与多个客户端建立网络连接,每个客户端可以向服务器发送命令请求,而服务器则接收并处理客户端发送的命令请求,并向客户端返回命令回复。通过使用由I/O多路复用技术实现的文件事件处理器,Redis服务器使用单线程单进程的方式来处理命令请求,并与多个客户端进行网络通信。核心实现概述对于每个与服务器进行连接的客户端,服务器都为这些客户端建立了相应的redis.h/redisClient结构(客户端状态),这个结构保存了

    2022年10月12日
    0
  • 手机gif录屏软件_手机录制gif软件

    手机gif录屏软件_手机录制gif软件gif录屏手机app是一款免费专业的视频剪辑制作软件,下载gif录屏apk支持GIF动画录屏以及MP4视频录屏,通过手机录屏GIF制作软件,一键轻松进行视频编辑、视频压缩和视频拼接。软件介绍gif录屏app是一款完全免费的专业高清流畅的录屏软件,支持GIF动画录屏以及MP4视频录屏。功能强大,操作简单,用户可以轻松录屏,录屏生成GIF回味用户自动生成GIF文件;将屏幕录制到动画文件.GIF,一边录…

    2022年9月20日
    0
  • java python哪个好_java和python哪个更好用?(一)[通俗易懂]

    java python哪个好_java和python哪个更好用?(一)[通俗易懂]JavaJava是世界上最古老,功能最强大的编程语言之一。它是一种通用的静态类型的语言。这意味着任何人都可以使用它。使用此编程语言没有特定的目的。Java还是一种面向对象的编程语言。这使其成为易于使用的编程语言之一。Java还是一种可移植的编程语言,可以在WORA上运行(一旦在任何地方运行,编写一次)。这意味着您可以在特定计算机上编写Java程序,并在任何平台上使用它。您需要拥有Java虚拟机(…

    2022年7月8日
    20
  • 基于SCADA数据驱动的风电机组部件故障预警

    基于SCADA数据驱动的风电机组部件故障预警

    2021年11月22日
    51
  • np.astype()

    np.astype()1.作用:就是转换numpy数组的数据类型举个例子arr=np.arange((10))print(arr,arr.dtype,sep=”\n”)===================================[0123456789]int32#可以看到,他的数据类型为int32np.astype()arr=arr.astype(“f…

    2022年6月10日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号