【经验分享】本地脚本工具 -- Html Application

扩展名为hta的文件即:网页应用程序(Html Application)
这类程序中网页拥有本地安全访问级别,可以创建ActiveX对象、访问内存、文件、注册表、网络等等

本文介绍使用hta编写工具可能碰到的技术,包括:访问文本文件、数据库、xml文件、网络等等

【优势】
不需要安装开发和运行环境
记事本即可编写
浏览器即可运行
WEB开发人员容易上手VBScript、JavaScript

例子1 "hello.hta" 显示对话框
  1. <html>
  2. <scripttype="text/javascript">
  3. alert("helloworld");
  4. </script>
  5. </html>
将内容存为"hello.hta"文件即可运行,后面的列子运行方式相同,不再说明
运行上面的程序,将显示出一个对话框,和普通网页完全一样

【访问文件】
例子2 "fileexists.hta" 判断文件目录是否存在
  1. <html>
  2. <body>
  3. <div>
  4. <inputid="TextFilename"type="text"value="c:/autoexec.bat"/>
  5. <inputtype="button"value="判断文件是否存在"onclick="ButtonFileExists();"/>
  6. </div>
  7. <div>
  8. <inputid="TextFolder"type="text"value="c:/windows"/>
  9. <inputtype="button"value="判断目录是否存在"onclick="ButtonFolderExists();"/>
  10. </div>
  11. <scripttype="text/javascript">
  12. var$=document.getElementById;
  13. functionButtonFileExists(){
  14. varfso=newActiveXObject("Scripting.FileSystemObject");
  15. alert(fso.FileExists($("TextFilename").value)?"文件存在":"文件不存在");
  16. fso=null;
  17. }
  18. functionButtonFolderExists(){
  19. varfso=newActiveXObject("Scripting.FileSystemObject");
  20. alert(fso.FolderExists($("TextFolder").value)?"目录存在":"目录不存在");
  21. fso=null;
  22. }
  23. </script>
  24. <body>
  25. </html>
在hat中复杂操作都离不开ActiveX对象,ActiveXObject()是创建该对象的方法。
参数"Scripting.FileSystemObject"则是这个对象注册的名字
在注册表里可以找到:
  1. HKEY_CLASSES_ROOT/Scripting.FileSystemObject/CLSID
  2. @={0D43FE01-F093-11CF-8940-00A0C9054228}
  3. ----
  4. HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}
  5. @=FileSystemObject
  6. ----
  7. HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}/InprocServer32
  8. @=C:/WINDOWS/system32/scrrun.dll
  9. ----
  10. HKEY_CLASSES_ROOT/CLSID/{0D43FE01-F093-11CF-8940-00A0C9054228}/ProgID
  11. @=Scripting.FileSystemObject
这样程序就可以知道实际从哪个dll里载入ActiveX对象
Scripting.FileSystemObject明显是一个操作文件系统的对象
详细资料参考: http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx

【访问不同编码的文本文件】
现在程序越来越国际化,所以Unicdoe、UTF8编码非常流行
这样在处理文本文件的时候就会碰到不同编码的情况
例子3 "readtextfile.hta" 读取文本文件
  1. <html>
  2. <body>
  3. <div>
  4. 文件名:<inputid="TextFilename"type="text"value="tempUnicode.txt"/>
  5. <inputtype="button"value="按gb2312读取"onclick="ButtonReadTextClick('gb2312');"/>
  6. <inputtype="button"value="按Unicode读取"onclick="ButtonReadTextClick('Unicode');"/>
  7. <inputtype="button"value="按UTF-8读取"onclick="ButtonReadTextClick('UTF-8');"/>
  8. </div>
  9. <textareaid="TextareaText"style="height:400px;width:100%;"></textarea>
  10. <scripttype="text/javascript">
  11. varBinaryStreamType=1;
  12. varTextStreamType=2;
  13. var$=document.getElementById;
  14. functionButtonReadTextClick(charset)
  15. {
  16. varadostream=newActiveXObject("ADODB.Stream");
  17. adostream.Type=TextStreamType;
  18. if(typeof(charset)=="undefined") charset=charset;
  19. adostream.Charset=charset;
  20. adostream.Open();
  21. adostream.LoadFromFile($("TextFilename").value);
  22. varcontents=adostream.ReadText();
  23. adostream.Close();
  24. adostream=null;
  25. $("TextareaText").value=contents;
  26. }
  27. </script>
  28. <body>
  29. </html>
Scripting.FileSystemObject可以处理Unicode编码但不能处理Utf8编码。
用ADODB.Stream则可以处理更多的编码;
该对象还可以按字节读取,可是我郁闷没有找到javascript中如何处理variant类型的方法,知道的请告之。
Variant = Stream .Read ( NumBytes )

【创建快捷方式】
例子4 "createshortcut.hta" 创建快捷方式
  1. <html>
  2. <title>快捷方式--清洁工2008版</title>
  3. <body>
  4. <div>
  5. <inputtype="button"value="开始"onclick="ButtonStartClick();"id="ButtonStart"/>
  6. </div>
  7. <scriptlanguage="javascript"type="text/javascript">
  8. var$=document.getElementById;
  9. functionButtonStartClick() {
  10. varshell=newActiveXObject("WScript.Shell");
  11. varstrDesktop=shell.SpecialFolders("Desktop");
  12. varlink=shell.CreateShortcut(strDesktop+"//一个记事本.lnk");
  13. link.TargetPath="notepad.exe";
  14. link.WorkingDirectory="";
  15. link.Save();
  16. varlink=shell.CreateShortcut(strDesktop+"//床上等你.url");
  17. link.TargetPath="http://www.youkuaiyun.com";
  18. link.Save();
  19. link=null;
  20. shell=null;
  21. }
  22. </script>
  23. </body>
  24. </html>
WScript.Shell是个好东东,shell.SpecialFolders("Desktop");这个方法就是获取桌面的路径
详细可以参考阿笨狗的网页: http://www.pifoo.com/post/WscriptShell.html

【访问XML】
XML目前是相当流行一种数据交互文件
例子5 "addxmlnode.hta" 添加节点
  1. <html>
  2. <body>
  3. <div>
  4. <inputtype="button"value="测试"onclick="ButtonClick();"/>
  5. </div>
  6. <textareaid="TextareaText"style="height:400px;width:100%;"></textarea>
  7. <scripttype="text/javascript">
  8. var$=document.getElementById;
  9. functionButtonClick(){
  10. varxmldocument=newActiveXObject("MSXML2.DOMDocument");
  11. xmldocument.loadXML("<?xmlversion=/"1.0/"?><root></root>");
  12. varroot=xmldocument.documentElement;
  13. varnewnode=root.appendChild(xmldocument.createElement("newnode"));
  14. newnode.setAttribute("att1","1");
  15. newnode.setAttribute("att1","2");
  16. newnode.text="zswang路过";
  17. $("TextareaText").value=xmldocument.xml;
  18. xmldocument=null;
  19. }
  20. </script>
  21. <body>
  22. </html>
xmldocument.loadXML()是直接载入xml文本
xmldocument.load(fileName)从文件中载入
xmldocument.save(fileName)从保存到文件

例子6 "xpathdemo.hta" xpath演示
  1. <html>
  2. <body>
  3. <textareaid="TextareaXML"style="width:100%;height:45%">
  4. <?xmlversion="1.0"?>
  5. <test>
  6. <xa="1">
  7. <xa="2"b="B">
  8. <x>
  9. <y>y31</y>
  10. <y>y32</y>
  11. <ya="3">y33</y>
  12. </x>
  13. </x>
  14. </x>
  15. <ya="4">y11</y>
  16. </test>
  17. </textarea>
  18. <textareaid="TextareaText"style="width:100%;height:45%"></textarea>
  19. <buttononclick="ButtonClick(this);"title="寻找Tag为y的节点">//y</button>
  20. <buttononclick="ButtonClick(this);"title="寻找存在属性a的节点">//*[@a]</button>
  21. <buttononclick="ButtonClick(this);"title="寻找文本为y31的节点">//y[.="y31"]</button>
  22. <buttononclick="ButtonClick(this);"title="寻找Tag为y并且序号3的节点">//y[position()=3]</button>
  23. <scriptlanguage="javascript">
  24. var$=document.getElementById;
  25. varxmldocument=newActiveXObject("MSXML2.DOMDocument");
  26. functionButtonClick(sender){
  27. xmldocument.loadXML($("TextareaXML").value);
  28. xmldocument.setProperty("SelectionLanguage","XPath");
  29. varnodes=xmldocument.selectNodes(sender.innerText);
  30. for(vari=0;i<nodes.length;i++)
  31. nodes[i].setAttribute("find","****");
  32. $("TextareaText").value=xmldocument.xml;
  33. }
  34. </script>
  35. </body>
  36. </html>
寻找xml节点最方便的就是使用xpath表达式

参考: http://www.w3.org/TR/xpath

【访问网络】
这个是ajax常用的“Microsoft.XMLHTTP”对象,当然本地安全级不用担心跨域问题。
这里就不多讲了,参考:
【脚本共享】优快云版主批量删帖工具。
优快云积分查询--清洁工2008版

【访问数据库】
使用ADODB.Connection链接数据。

例子7 "Access.hta" 创建Access数据库和执行插入和查询
  1. <html>
  2. <title>测试</title>
  3. <div>
  4. <inputtype="button"value="测试"onclick="ButtonStartClick();"id="ButtonStart"/>
  5. </div>
  6. <divstyle="width:100%;height:80%">
  7. <textareaid="TextareaLog"style="width:100%;height:100%"></textarea>
  8. </div>
  9. <scriptlanguage="javascript"type="text/javascript">
  10. var$=document.getElementById;
  11. varaccessfile="temp.mdb";
  12. varadSchemaTables=20;
  13. functionButtonStartClick(){
  14. varcreateDatabase=false;
  15. ///Begin确保数据库文件存在
  16. varfso=newActiveXObject("Scripting.FileSystemObject");
  17. if(!fso.FileExists(accessfile)){
  18. varadoxcatalog=newActiveXObject("ADOX.Catalog");
  19. try{
  20. adoxcatalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+accessfile);
  21. createDatabase=true;
  22. }catch(e){
  23. return;
  24. }
  25. adoxcatalog=null;
  26. }
  27. ///End确保数据库文件存在
  28. ///Begin确保表存在
  29. varexistsTable=false;
  30. varconnection=newActiveXObject("ADODB.Connection");
  31. try{
  32. connection.Open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+accessfile);
  33. if(!createDatabase){
  34. varrecordset=connection.OpenSchema(adSchemaTables);
  35. recordset.MoveFirst();
  36. while(!recordset.EOF){
  37. if(recordset("TABLE_TYPE")=="TABLE"){
  38. existsTable=true;
  39. break;
  40. }
  41. recordset.MoveNext();
  42. }
  43. recordset.Close();
  44. recordset=null;
  45. }
  46. if(!existsTable){
  47. connection.Execute("CREATETABLEtemp(记录号CounterPrimarykey,名称Text(10))");
  48. connection.Execute("INSERTINTOtemp(名称)values(/"zswang/")");
  49. connection.Execute("INSERTINTOtemp(名称)values(/"hcat1999/")");
  50. connection.Execute("INSERTINTOtemp(名称)values(/"ming4098/")");
  51. }
  52. }catch(e){
  53. return;
  54. }
  55. ///End确保表存在
  56. varrecordset=newActiveXObject("ADODB.Recordset");
  57. try{
  58. recordset.Open("SELECT*FROMtemp",connection);
  59. recordset.MoveFirst();
  60. while(!recordset.EOF){
  61. $("TextareaLog").value+=recordset("名称")+"/r/n";
  62. recordset.MoveNext();
  63. }
  64. recordset.Close();
  65. recordset=null;
  66. }catch(e){
  67. return;
  68. }
  69. connection.Close();
  70. }
  71. </script>
  72. </html>
【调用其他程序】

例子8 "runnotepad.hta" 打开记事本
  1. <html>
  2. <body>
  3. <buttononclick="ButtonClick();">打开记事本</button>
  4. <scripttype="text/javascript">
  5. varSW_HIDE=0;
  6. varSW_NORMAL=1;
  7. varSW_MINIMIZED=2;
  8. varSW_MAXIMIZED=3;
  9. varSW_NOACTIVATE=4;
  10. varSW_SHOW=5;
  11. varSW_MINIMIZE=6;
  12. varSW_MINNOACTIVE=7;
  13. varSW_SHOWNA=8;
  14. varSW_RESTORE=9;
  15. varSW_DEFAULT=10;
  16. functionButtonClick(){
  17. varshell=newActiveXObject("WScript.Shell");
  18. shell.Run("notepad.exe",SW_MAXIMIZED,true);//执行并等待
  19. shell=null;
  20. }
  21. </script>
  22. </body>
  23. </html>
例子9 "processes.hta" 遍历当前进程
  1. <html>
  2. <body>
  3. <script>
  4. try{
  5. varlocator=newActiveXObject("WbemScripting.SWbemLocator");
  6. varservice=locator.ConnectServer(".","/root/CIMV2");
  7. varprocesses=service.ExecQuery("Select*fromWin32_Process");
  8. varprocessEnum=newEnumerator(processes);
  9. varmsg="";
  10. for(;!processEnum.atEnd();processEnum.moveNext())
  11. {
  12. varprocess=processEnum.item();
  13. msg+=process.Name+"<br/>";
  14. }
  15. document.write(msg);
  16. }
  17. catch(e){
  18. }
  19. </script>
  20. </body>
  21. </html>
例子10 "doscmd.hta" 执行dos命令
  1. <html>
  2. <body>
  3. <buttononclick="ButtonClick();">设置NTFS文件访问权限</button>
  4. <scripttype="text/javascript">
  5. varSW_HIDE=0;
  6. varSW_NORMAL=1;
  7. varSW_MINIMIZED=2;
  8. varSW_MAXIMIZED=3;
  9. varSW_NOACTIVATE=4;
  10. varSW_SHOW=5;
  11. varSW_MINIMIZE=6;
  12. varSW_MINNOACTIVE=7;
  13. varSW_SHOWNA=8;
  14. varSW_RESTORE=9;
  15. varSW_DEFAULT=10;
  16. functionButtonClick(){
  17. varshell=newActiveXObject("WScript.Shell");
  18. shell.Run("cmd.exe/ccopyc://AUTOEXEC.BATd://AUTOEXEC.TXT",SW_HIDE,true);//执行并等待
  19. shell.Run("cmd.exe/ccaclsd://AUTOEXEC.TXT/e/p/"localservice/":w",SW_HIDE,true);//执行并等待
  20. shell=null;
  21. }
  22. </script>
  23. </body>
  24. </html>



<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="PowerPoint.Slide"> <meta name="Generator" content="Microsoft PowerPoint 11"> <!--[if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} p/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} v/:textbox {display:none;} </style> <![endif]-->幻灯片 2<meta name="Description" content="2008-11-1"> <!--[if !ppt]--><style> .O {font-size:149%;} </style> <style media="print"> <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} --> </style> <!--[endif]--><shapelayout v:ext="edit"></shapelayout><idmap v:ext="edit" data="1"></idmap><colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"><div v:shape="_x0000_s1026" style=""> <div class="O"> <span style="font-family: 华文新魏; font-size: 133%;"><span style="position: absolute; left: -4.02%; font-family: Wingdings;">u</span></span><span style="font-family: 华文新魏; font-size: 24pt;"></span> </div> <div class="O" style="text-align: center;"></div> </div> </colorscheme>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值