VBS WshShell.AppActivate无法激活cmd的解决方法


Set WshShell = CreateObject("WScript.Shell")
WshShell.run"cmd /k title tututu"
WScript.Sleep 2000
WshShell.AppActivate("123.txt")
WScript.Sleep 2000
WshShell.AppActivate("tututu")
WshShell.SendKeys "adb root"
WshShell.SendKeys "{ENTER}"

123.txt为提前打开的记事文本
上述代码执行后预期:
1.打开cmd窗口,标题改为tututu
2.等待2s
3.123.txt被激活选中
4.等待2s
5.cmd窗口被激活选中
6.cmd窗口输入adb root并按回车

实际结果:
1.打开cmd窗口,标题改为tututu
2.等待2s
3.123.txt被激活选中
4.等待2s
5.cmd窗口并没有被激活选中,但是能看到有光标在闪烁

原因:不详

解决办法:
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.run"cmd /k title tututu"
WshShell.SendKeys “adb root”
WshShell.SendKeys “{ENTER}”
WScript.Sleep 2000
WshShell.AppActivate(“123.txt”)
WScript.Sleep 2000
WshShell.AppActivate(“tututu”)
WshShell.SendKeys “adb root”
WshShell.SendKeys “{ENTER}”

第二种情况:

cmd已经打开,需要用新的vbs脚本激活已打开的cmd窗口

Set WshShell = CreateObject("WScript.Shell")
WshShell.run"cmd /c"  //这句话最重要,不然无法激活
WScript.sleep 1000
WshShell.AppActivate("tututu")

WshShell.SendKeys "adb root"
WshShell.SendKeys "{ENTER}"

资料:https://www.thinbug.com/q/52381791
Wscript.sleep将使情况变得更糟。你不想睡觉。

有规则。您的程序必须遵守以下规则之一才能设置前景窗口。

来自https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow

系统限制可以设置前景窗口的进程。仅当满足以下条件之一时,进程才能设置前景窗口:

•该过程是前台过程。

•该进程由前台进程启动。

•该进程收到了最后一个输入事件。

•没有前台进程。

•该进程正在调试中。

•前台过程不是现代应用程序或启动程序 屏幕。

•前景未锁定(请参见LockSetForegroundWindow)。

•前景锁定超时已到期(请参阅 SystemParametersInfo中的SPI_GETFOREGROUNDLOCKTIMEOUT。)

•没有菜单处于活动状态。

当用户使用时,应用程序无法将窗口强制到前台 正在使用另一个窗口。而是,Windows闪烁任务栏 窗口中的按钮以通知用户。

这意味着如果脚本没有用户界面(因为它不能是前台窗口),那么从脚本开始设置另一个窗口起,您有两秒钟的时间(FOREGROUNDLOCKTIMEOUT)。一个消息框是您要做的第一件事(直到发生其他事情)。另外Wscript的消息框超时-WshShell.Popup。

这是为了防止用户感到无聊并且现在正在使用另一个程序时启动缓慢的程序,从而失去了焦点。

预防性评论通常,人们会想与我争论。向微软投诉不是我。而且你不能与规则争论。您可以遵守它们。

Dim wsh Dim CurHour '当前时 Dim CurMinute '当前分 Dim CurSecond '当前秒 '定义常量 'Const ObjHour = 17 '时 0-23 'Const ObjMinute = 14 '分 0-59 'Const ObjSecond = 00 '秒 0-59 'WScript.Sleep 20000 ' 延时 20 秒 'set wshshell=CreateObject("wscript.shell") ' 创建并设置运行界面窗口 Set objWnd = wshshell.Exec("mshta.exe ""about:<title>running</title><body bgcolor=white><center><h1>Running</h1><p>Automatically press the TAB key every 3 minutes.<br> Please Close window to stop running</p></center><script>window.resizeTo(800,400);window.moveTo((screen.width-800)/2,(screen.height-400)/2);</script>""") set wshshell=CreateObject("wscript.shell") browserPath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" ' 浏览器的绝对路径 url1 = "http://192.168.10.81/decision/view/form?viewlet=%25E5%2588%25B6%25E9%2580%25A02%25E9%2583%25A8%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%25E4%25BA%25BA%25E5%2591%2598%25E7%25AE%25A1%25E7%2590%2586%25E6%259D%25BF%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%25E4%25BA%25BA%25E5%2591%2598%25E7%25AE%25A1%25E7%2590%2586%25E6%259D%25BF.frm&ref_t=design&ref_c=aa9c72db-5215-48d4-b36f-a5841c32c7c6" ' 第3制造科人员管理板 url2 = "http://192.168.154.11:4000/gridWebAndon/revoAssembly_Allparts" ' #12总装安东页面 url3 = "http://192.168.154.11:5000/gridWebAndon/revoAllparts" ' #12加工安东页面 wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url1 & Chr(34) wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url2 & Chr(34) wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url3 & Chr(34) WScript.Sleep 20000 ' 延时 20 秒 i = 5 do while(i>0) windowTitle = wshShell.AppActivate("Microsoft Edge") wScript.Sleep 200 wshShell.SendKeys "% " wScript.Sleep 200 wshShell.SendKeys "r" i = i-1 loop 'MsgBox windowTitle WScript.Sleep 100 ' 延时 0.1 秒 wshShell.SendKeys "{esc}" WScript.Sleep 100 ' 延时 0.1 秒 wshshell.SendKeys "{F11}" WScript.Sleep 5000 ' 延时 5 秒 wshshell.SendKeys "{F5}" ' 当窗口关闭时退出循环 'do While (objWnd.Status = 0) 'CurHour = Hour(Now) 'CurMinute = Minute(Now) 'CurSecond = Second(Now) 'WScript.Sleep 60000 ' 切换延时60秒 'if CurHour = ObjHour and CurMinute >= ObjMinute then 'CreateObject("Wscript.Shell").run "cmd.exe /c shutdown -s -f" 'end if 'wshshell.SendKeys "^{TAB}" 'Loop ' 退出脚本 'WScript.Quit
08-09
Dim wsh Dim CurHour '当前时 Dim CurMinute '当前分 Dim CurSecond '当前秒 '定义常量 Const ObjHour = 17 '时 0-23 Const ObjMinute = 14 '分 0-59 Const ObjSecond = 00 '秒 0-59 WScript.Sleep 20000 ' 延时 20 秒 set wshshell=CreateObject("wscript.shell") ' 创建并设置运行界面窗口 Set objWnd = wshshell.Exec("mshta.exe ""about:<title>running</title><body bgcolor=white><center><h1>Running</h1><p>Automatically press the TAB key every 3 minutes.<br> Please Close window to stop running</p></center><script>window.resizeTo(800,400);window.moveTo((screen.width-800)/2,(screen.height-400)/2);</script>""") set wshshell=CreateObject("wscript.shell") browserPath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" ' 浏览器的绝对路径 url1 = "http://192.168.10.81/decision/view/form?viewlet=%25E5%2588%25B6%25E9%2580%25A02%25E9%2583%25A8%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%25E4%25BA%25BA%25E5%2591%2598%25E7%25AE%25A1%25E7%2590%2586%25E6%259D%25BF%252F%25E7%25AC%25AC3%25E5%2588%25B6%25E9%2580%25A0%25E7%25A7%2591%25E4%25BA%25BA%25E5%2591%2598%25E7%25AE%25A1%25E7%2590%2586%25E6%259D%25BF.frm&ref_t=design&ref_c=aa9c72db-5215-48d4-b36f-a5841c32c7c6" ' 生产日报页面 url2 = "http://192.168.154.11:4000/gridWebAndon/revoAssembly_Allparts" ' #12总装安东页面 url3 = "http://192.168.154.11:5000/gridWebAndon/revoAllparts" ' #12加工安东页面 wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url1 & Chr(34) wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url2 & Chr(34) wshshell.Run Chr(34) & browserPath & Chr(34)& " " & Chr(34)& url3 & Chr(34) WScript.Sleep 10000 ' 延时 10 秒 i = 5 do while(i>0) windowTitle = wshShell.AppActivate("Microsoft Edge") wScript.Sleep 200 wshShell.SendKeys "% " i = i-1 loop 'MsgBox windowTitle WScript.Sleep 100 ' 延时 0.1 秒 wshShell.SendKeys "{esc}" WScript.Sleep 100 ' 延时 0.1 秒 wshshell.SendKeys "{F11}" WScript.Sleep 5000 ' 延时 5 秒 wshshell.SendKeys "{F5}" ' 当窗口关闭时退出循环 do While (objWnd.Status = 0) CurHour = Hour(Now) CurMinute = Minute(Now) CurSecond = Second(Now) WScript.Sleep 60000 ' 切换延时60秒 if CurHour = ObjHour and CurMinute >= ObjMinute then CreateObject("Wscript.Shell").run "cmd.exe /c shutdown -s -f" end if wshshell.SendKeys "^{TAB}" Loop ' 退出脚本 WScript.Quit 删除关机代码
最新发布
08-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值