插入到关闭窗口不循环的窗口



我有一个通常需要运行的应用程序,而无需积极监控,但是,如果遇到错误,它将弹出消息窗口并暂停,直到用户按下关闭。目前,由于我们正在压力测试,并且无法禁用这些消息,因此它正在引起很多问题,因此有人必须不断地关注该过程。我一直在尝试提出一些VBScript,这些VBScript将继续检查弹出消息并使用AppActivateSendKeys函数自动关闭它。

问题是我需要在GUI中工作,以便用户可以在单击按钮时轻松启动/停止代码运行。因此,我目前将我的vbscript包裹在一个带有一个按钮的HTA应用程序中,但是由于某种原因,它只会关闭消息的第一个实例(多个可以同时加载多个),然后停止。

Sub Testing
    Set objShell = CreateObject("WScript.Shell")
    counter = 1
    Do While counter < 50
        ret = objShell.AppActivate("Test Failure")
        counter = counter + 1
        If ret = True Then
            objShell.SendKeys "~"
        End If
    Loop
End Sub

使用按钮如下:

<input type="button" value="Run Script" name="run_button" onClick="Testing"><p>

我需要它只是在后台不断运行,仅在选择"测试失败"窗口/存在时才执行SendKeys,并且在按下第二个按钮时能够停止循环。

我显然遇到了一些问题(VBScript确实执行了上下,不是吗?


我已经标记了@ansgar Wiechers的回答,就像解决了我的问题的根本原因一样正确。我最终提出的最终代码分为两个部分。应用程序:

<!doctype html>
<head>
<hta:application
    id="AutoCloseFailures"
    applicationname="AutoCloseFailures"
    icon=""
    singleinstance="yes"
    border="thick"
    borderstyle="complex"
    scroll="no"
    maximizebutton="no"
    version="0.1" />
<title>Auto-Close Failure Messages</title>
<meta http-equiv="x-ua-compatible" content="ie=8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="VBScript">
    ' Set Global Variables for Subroutines
    script = "#FOLDERPATH#AutoCloseDEFXErrorsTimer.vbs"
    Set sh  = CreateObject("WScript.Shell")
    Set cmdTest = Nothing
    strVar = "testing2"
    Sub StartLoop
    ' Initiate VB Loop to Target Failure Windows & Close Them
        If cmdTest Is Nothing Then
            Set cmdTest = sh.Exec("wscript.exe """ & script & """")
            strVar = "testing"
        End If
        document.all.start.style.background="#777d84"
        document.all.start.style.color="#bfc4c9"
    End Sub
    Sub StopLoop
    ' Terminate VB Loop Process & Reset Application
        strScriptToKill = "AutoCloseDEFXErrorsTimer.vbs"
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\.rootcimv2")
        Set colProcess = objWMIService.ExecQuery ( _
            "Select * from Win32_Process " & _
            "WHERE (Name = 'cscript.exe' OR Name = 'wscript.exe') " & _
            "AND Commandline LIKE '%"& strScriptToKill &"%'" _
        )
        For Each objProcess in colProcess
            objProcess.Terminate()
        Next
        Set cmdTest = Nothing
        document.all.start.style.background="#587286"
        document.all.start.style.color="#ffffff"
    End Sub
    Sub Window_onLoad
    ' Force Window Size
        window.resizeTo 400,190
        window.moveTo 1530, 0
    End Sub
</script>
<style>
    body {
        margin: 0;
        padding: 0;
        font-family: "Segoe UI", Geneva, sans-serif;
        background: #dae3f2;
    }
    h1 {
        font-size: 15pt;
        text-align: center;
        margin-bottom: 0;
        color: #273754;
    }
    button {
        padding:16px 32px;
        font-family:helvetica;
        font-size:16px;
        font-weight:100;
        color:#fff;
        background: #587286;
        border:0;
    }
    button:hover {
        background: #3B5C76;
    }
    .container {
        width: 100%;
        text-align: center;
    }
</style>
<BODY>
    <h1>Auto-Close Failure Messages</h1>
    <br>
        <div class="container">
            <button id="start" onclick="StartLoop">Start</button>
            <span>&nbsp;&nbsp;&nbsp;</span>
            <button id="stop" onclick="StopLoop">Stop</button>
        </div>
    <br>
</BODY>
</HTML>

和支持的VBScript文件:

Set sh = CreateObject("WScript.Shell")
While True
    active = sh.AppActivate("#WINDOW TITLE#")
    If active Then
        sh.SendKeys "~"
    End If
    WScript.Sleep 100
Wend

您的过程正在毫不拖延地进行50次迭代,因此它甚至可能已经完成了第二个实例。您通常要做的就是在无限延迟的无限环中运行AppActivateSendKeys

Set sh = CreateObject("WScript.Shell")
While True
    active = sh.AppActivate("Test Failure")
    If active Then
        sh.SendKeys "~"
    End If
    WScript.Sleep 100
Wend

将其放在您从HTA上异步运行的脚本:

<html>
<head>
<title>sample</title>
<HTA:APPLICATION
    ID="sample"
    APPLICATIONNAME="oHTA"
    SINGLEINSTANCE="yes">
<script language="VBScript">
script = "C:pathtoscript.vbs"
Set sh  = CreateObject("WScript.Shell")
Set cmd = Nothing
Sub StartLoop
    If cmd Is Nothing Then
        Set cmd = sh.Exec("wscript.exe """ & script & """")
    End If
End Sub
Sub StopLoop
    If Not cmd Is Nothing Then
        cmd.Terminate
        Set cmd = Nothing
    End If
End Sub
</script>
</head>
<body>
<p>
<button id="start" onclick="StartLoop">Start</button>
<button id="stop" onclick="StopLoop">Stop</button>
</p>
</body>
</html>

如果需要,您可以即时从HTA创建外部脚本:

Set fso = CreateObject("Scripting.FileSystemObject")
...
Set f = fso.OpenTextFile(script, 2, True)
f.WriteLine "Set sh = CreateObject(""WScript.Shell"")"
f.WriteLine "While True"
f.WriteLine "active = sh.AppActivate(""Test Failure"")"
...
f.Close

并在终止后删除它:

If Not cmd Is Nothing Then
    cmd.Terminate
    Set cmd = Nothing
    fso.DeleteFile script, True
End If

相关内容

  • 没有找到相关文章

最新更新