赵翔鹏的Blog Xiangpeng's Thinkpad

3十二/040

几个用Sendkeys做的脚本(zz)

下面是几个SendKeys的例子。关于Wscript和SendKeys,我还写过两篇相关的blog:
SendKeys参考文档在windows下编辑代码,用linux远程编译.

-----
我們可以使用 sendKeys() 函數來傳送鍵盤事件,在下面這個範例,我們開啟 IE 並執行列印的動作:

原始檔(printIE.js):(灰色區域按兩下即可拷貝)
// 由 IE 列印網頁
objMyIE = WScript.CreateObject("InternetExplorer.Application");

URL="http://neural.cs.nthu.edu.tw/jang";
//URL="http://www.google.com";
//URL="file://d:/users/jang/books/wsh/example/test.htm";
objMyIE.Navigate(URL);
objMyIE.visible = true;

while (objMyIE.Busy)
 WScript.Sleep(1000);

WshShell=new ActiveXObject("WScript.Shell");
WshShell.SendKeys("%{f}");
WScript.Sleep(1000);
WshShell.SendKeys("p");
WScript.Sleep(1000);
WshShell.SendKeys("%{p}");

//objMyIE.document.parentWindow.Print(); // 有 bug!
//objMyIE.Quit();


我們也可以開啟記事本並寫入文字:

原始檔(sendKeys01.js):(灰色區域按兩下即可拷貝)
// 使用 SendKeys 去開啟記事本並寫入文字、存檔

WshShell=new ActiveXObject("WScript.Shell");

WshShell.Run("notepad", 9);
WScript.Sleep(500); // Give Notepad some time to load
for (i=0; i<10; i++){
 WshShell.SendKeys(i+". Hello World!");
 WshShell.SendKeys("{ENTER}");
}
WshShell.SendKeys("%{F}");
WshShell.SendKeys("s");
WshShell.SendKeys("test.txt");
WshShell.SendKeys("{TAB}{TAB}{ENTER}");
WshShell.SendKeys("y");
WshShell.SendKeys("%{F4}");


若要開啟 IE 並設定我的最愛,可見下列範例:

原始檔(sendKeys02.js):(灰色區域按兩下即可拷貝)
// 設定我的最愛(還有一些小問題!)
WshShell=new ActiveXObject("WScript.Shell");

WshShell.Run("iexplore", 9);
WScript.Sleep(1000); // Give Notepad some time to load
WshShell.SendKeys("%t");
WshShell.SendKeys("o");
WScript.Quit(1);
//WshShell.SendKeys("http://neural.cs.nthu.edu.tw/jang");
WshShell.SendKeys("http://www.cs.nthu.edu.tw");
WScript.Quit(1);

for (i=0; i<13; i++)
 WshShell.SendKeys("{TAB}");
WScript.Quit(1);
WshShell.SendKeys("{ENTER}");
评论 (0) 引用 (0)

还没有评论.


Leave a comment

(required)

还没有引用.