24-10 �ǰe��L�ƥ�

§Ú­Ì¥i¥H¨Ï¥Î sendKeys() ¨ç¼Æ¨Ó¶Ç°eÁä½L¨Æ¥ó¡A³o¬O¤@­Ó«Ü±j¤jªº¥\¯à¡A¥ô¦ó³nÅé¥u­n¯à°÷¤ä´©¨Ï¥Î¼öÁä¨Ó¾Þ§@¡A§Ú­Ì´N¥i¥H¨Ï¥Î WSH ¨Ó¹F¨ì¤@¼Ò¤@¼Ëªº¥\¯à¡C

¦b¤U­±³o­Ó½d¨Ò¡A§Ú­Ì¶}±Ò IE ¨Ã°õ¦æ¦C¦Lªº°Ê§@¡G

­ì©lÀÉ¡]iePrint01.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¶}±Ò IE ¨Ã°õ¦æ¦C¦L
// Roger Jang, 20081215, tested under Vista

// Step 1: ¶}±Ò IE ¨Ã¸ü¤J Google ·j´Mºô­¶
WshShell=new ActiveXObject("WScript.Shell");
WScript.Echo('Will open IE in 5 sec...'); WScript.Sleep(5000);
WshShell.Run("iexplore", 9);

// Step 2: ¶i¦æ¦C¦L°Ê§@
WScript.Echo('Will click alt-d in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{d}");
WScript.Echo('Will send URL in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("http://www.google.com");
WScript.Echo('Will click alt-f in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{f}");
WScript.Echo('Will click p in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("p");
WScript.Echo('Will click enter in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("{ENTER}");
WScript.Echo('Will close the browser in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{F4}");

½Ðª`·N¦b¤W­z½d¨Ò¤¤¡A

¥H¤U¦C¥X¤@¨Ç¯S®í«öÁ䪺¬ÛÃö©I¥s¤è¦¡¡G
«öŲ ©I¥s¤è¦¡
SHIFT +
CONTROL ^
ALT %
LEFT ARROW {LEFT}
RIGHT ARROW {RIGHT}
UP ARROW {UP}
DOWN ARROW {DOWN}
{{{}
} {}}
[{[}
] {]}
~ {~}
+ {+}
^ {^}
% {%}
«öŲ ©I¥s¤è¦¡
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
END {END}
ENTER {ENTER} or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
«öŲ ©I¥s¤è¦¡
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}

§Ú­Ì¤]¥i¥H¶}±Ò°O¨Æ¥»¨Ã¼g¤J¤å¦r¡G

­ì©lÀÉ¡]sendKeys01.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¨Ï¥Î SendKeys ¥h¶}±Ò°O¨Æ¥»¨Ã¼g¤J¤å¦r¡B¦sÀÉ©ó junk.txt
// Roger Jang, 20081215, tested under Vista

outputFile="junk.txt";
WshShell=new ActiveXObject("WScript.Shell");
WshShell.Run("notepad", 9);	// Open notepad
WScript.Sleep(1000);		// Give Notepad some time to load
for (i=0; i<100; i++){
	WshShell.SendKeys(i+". Hello World!");
	WshShell.SendKeys("{ENTER}");
}
WshShell.SendKeys("%{F}");
WshShell.SendKeys("s");
WScript.Echo('Will input the file name in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys(outputFile);
//WshShell.SendKeys("{TAB}{TAB}{ENTER}");
WScript.Echo('Will save the file in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{s}");
WScript.Echo('Will click yes in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{y}");
WScript.Echo('Will close the file in 5 sec...'); WScript.Sleep(5000);
WshShell.SendKeys("%{F4}");

¤W­z½d¨Ò¬O¤@­Ó«Ü¦³½ìªº½d¨Ò¡AŪªÌ¤@©w­n¿Ë¦Û¸Õ¬Ý¬Ý¡A¤~¯àÅé·|¨Ï¥ÎWSHªº¡u¶Ç°eÁä½L¨Æ¥ó¡v¥i¥H¹F¨ìªº±j¤j¥\¯à¡I

Hint
¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥i¥H±N­^¤å¼g¤J°O¨Æ¥»¡A¦ý¬O­Y­n±N¤¤¤å°e¤J¡A´Nµ§ªÌªº¸ÕÅç¨Ó»¡¡A¥Ø«e·|¥X²{¶Ã½X¡C¤@­Ó²³æªº¸Ñ¨M¤è®×¡A´N¬O¥ý±N¤¤¤å°e°Å¶Kï¡A¦A¶K¨ì°O¨Æ¥»§Y¥i¡C

­Y­n¶}±Ò IE ¨Ã³]©w¹w³]ºô­¶¡A¥i¨£¤U¦C½d¨Ò¡G

­ì©lÀÉ¡]sendKeys02.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ³]©w IE ªº¹w³]ºô­¶
// Roger Jang, 20081215, tested under Vista

WshShell=new ActiveXObject("WScript.Shell");
WshShell.Run("iexplore", 9);
WScript.Sleep(5000);		// µ¥«Ýºô­¶¸ü¤J
WshShell.SendKeys("%t");
WshShell.SendKeys("o");
WScript.Sleep(500);
WshShell.SendKeys("http://www.google.com");
WScript.Sleep(500);
for (i=0; i<13; i++)	// Make sure it's 13...
	WshShell.SendKeys("{TAB}");
WshShell.SendKeys("{ENTER}");
WScript.Sleep(500);

¦b¤U­z½d¨Ò¤¤¡A§Ú­Ì§Q¥Î°O¨Æ¥»Åª¤J¤@­Ó¤å¦rÀÉ¡A±N¤å¦rÀɪº½s½X¤è¦¡§ï¦¨ UTF-8¡A¨Ã¥t¦s·sÀÉ¡A¦p¤U¡G

­ì©lÀÉ¡]toUtf8.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¥Îªk¡Gcscript toUtf8.js file1 file2 file3 ...
// ¥\¯à¡G±N command line ©Òµ¹ªº¤å¦rÀɧQ¥Î notepad Âন utf-8 ªº®æ¦¡
// »¡©ú¡G
// 	¦pªG¿é¤JÀɦW¬O test.txt¡A¿é¥XÀɦW«h¬O text.txt_utf8
// 	ª`·N¡G¦pªG¿é¤JÀɦW¬O¬Û¹ï¸ô®|¡A«h¦b¨Ï¥Î«e¡A¥²»Ý½T»{ notepad ªº¹w³]Àx¦s¥Ø¿ý¬O¥¿½Tªº¥Ø¿ý
// 	¡]­Y¨Ï¥Îµ´¹ï¸ô®|¡A«h¨S¦³¤W­zÅU¼{¡C¡^

// 	Roger Jang, 20041125

args=WScript.Arguments;
if (args.Count()==0) {
	WScript.Echo("Usage: " + WScript.ScriptName + " file1 file2 file3 ...");
	WScript.Quit();
}

fso = new ActiveXObject("Scripting.FileSystemObject");

WshShell=new ActiveXObject("WScript.Shell");
WScript.Echo("Current directory: "+WshShell.CurrentDirectory);

// ¦C¥X©Ò¦³ªº¿é¤J°Ñ¼Æ
WScript.Echo("No. of arguments = " + WScript.Arguments.Count());
for (i=0; i<args.length; i++){
	WScript.Echo("args("+i+")="+args(i));
	targetFile=args(i)+"_utf8";
	WScript.Echo("targetFile="+targetFile);
	// Delete the targetFile if it exists
	if (fso.FileExists(targetFile)){
		WScript.Echo("Deleting "+targetFile);
		f = fso.GetFile(targetFile);
		f.Delete();
		WScript.Sleep(500);
	}

	WshShell.Run("notepad "+args(i), 9);
	WScript.Sleep(500);	// Give Notepad some time to load
	WshShell.SendKeys("%{F}");
	WshShell.SendKeys("a");
	WshShell.SendKeys(targetFile);
	WScript.Sleep(5000);
	WshShell.SendKeys("{TAB}{TAB}{TAB}");
	WshShell.SendKeys("{DOWN}{DOWN}{DOWN}{DOWN}");
	WshShell.SendKeys("{TAB}");
	WshShell.SendKeys("{ENTER}");
	WshShell.SendKeys("%{F4}");
	WScript.Sleep(1000);

	//§ïÀɦW¡Gtest.txt_utf8.txt ===> test.txt_utf8
//	fso.MoveFile(targetFile+".txt", targetFile);
	WScript.Sleep(1000);
}

­Y­n´ú¸Õ¤U¦C½d¨Ò¡AŪªÌ¥i¥H¿é¤J

cscript toUtf8.js test.txt ¦¹®É WSH ·|¨Ï¥Î°O¨Æ¥»¨ÓŪ¤J test.txt¡A±N¨ä½s½X§ï¬° UTF-8¡A¨Ã¥t¦s¦¨ test.txt_utf8¡C­Y­nÀ˵ø³o¨â­ÓÀɮתº¤£¦P¡A¥i¥Îºô­¶ÂsÄý¾¹¨ÓÅã¥Ü³o¨â­ÓÀɮסA¨Ã¥Ñ¤U©Ô¿ï³æ¡uÀ˵ø/½s½X¡v¡A´N¥i¥H¬Ý¥X³o¨â­ÓÀɮצb½s½X¤Wªº¤£¦P¡C

Hint
§Q¥Î UTF-8 ªº½s½X¤è¦¡¡A´N¥i¥H¦P®ÉÅã¥Ü¦U°ê¤£¦Pªº¤å¦r©ó¦P¤@­Ó¤å¦rÀɮסC

¨ä¥L»P¶Ç°eÁä½L¨Æ¥óªº¬ÛÃö»¡©ú¡A¥i¨£·L³nªº©x¤èºô­¶¡G http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_hilv.mspx?mfr=true


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó³æ¾÷ªº WSH Àô¹Ò