2014-06-22 35 views
0

我想學習JScript(不是JavaScript),但我有一個時間尋找資源的地獄。我想要做的是操縱windows shell創建一個程序來操縱其他程序,所以我特別需要能夠使用Jscript的AppActivate()函數在Windows之間切換焦點,但它不起作用。我假設它不工作,因爲調用AppActivate發生在我試圖操縱完全加載的窗口之前?WSH Jscript AppActivate()和睡眠()

可能有人請:

給我之間的發言權,計算器和記事本的切換工作的例子,也給我一些很好的參考材料?

此外,如何在執行結束時暫停程序,以便我可以在命令提示符下讀取錯誤?

這就是我寫的,每次都崩潰。

import System; 
import System.Drawing; 
import System.Windows.Forms; 
import Accessibility; 


//Open calculator, wait 3 seconds, open notepad, wait three seconds, change focus to calculator. 
var WshShell = new ActiveXObject("WScript.Shell"); 
WshShell.Run("calc"); 
WshShell.Sleep(3000); 
WshShell.Run("c:/windows/system32/notepad.exe"); 
WshShell.Sleep(3000); 
AppActivate("calc"); 
+0

以下是一些資源:http://www.limm.mgimo.ru/doc/jscript/htm/jstutor.htm,http://www.petergottlieb.com/docs/spect/DOASIS/jscript_tutorial.pdf –

回答

4

腳本的第一行(import ...)不是WSH JScript。刪除它們。 WScript.Shell對象沒有.Sleep方法。該方法由(全局)WScript對象提供。要使用.AppActivate,您需要指定WScript.Shell對象。

工作代碼:

var WshShell = new ActiveXObject("WScript.Shell"); 
WshShell.Run("calc"); 
WScript.Sleep(3000); 
WshShell.Run("c:/windows/system32/notepad.exe"); 
WScript.Sleep(3000); 
WshShell.AppActivate("calc"); 

要看看發生了什麼事情,從一個控制檯( 'DOS窗口')開始你的腳本。

開始閱讀here.

更新WRT評論:

WSH JScript是不是.NET的JScript。一個上面的腳本到.NET的「端口」的樣子:

import System; 

var WshShell = new ActiveXObject("WScript.Shell"); 
WshShell.Run("calc"); 
System.Threading.Thread.Sleep(3000); 
WshShell.Run("c:/windows/system32/notepad.exe"); 
System.Threading.Thread.Sleep(3000); 
WshShell.AppActivate("calc"); 
System.Threading.Thread.Sleep(3000); 
WshShell.AppActivate("notepad"); 

正如你所看到的,你需要使用平臺(WSH VS .NET)的睡眠功能。

+0

好吧,這將打開一個計算器的實例,但只要遇到睡眠()我的程序崩潰。是什麼賦予了? – user3764918

+0

@ user3764918 - 請發佈錯誤消息。 –

+0

錯誤消息:「[程序名稱] .exe」已停止工作檢查解決方案「,然後」問題導致程序無法正常工作。 Windows將關閉程序並通知您是否有解決方案「 – user3764918