AppActivate关闭窗口不循环

时间:2017-02-23 09:15:38

标签: vbscript hta

我有一个应用程序,经常需要在没有主动监视的情况下运行,但是,如果遇到错误,它将弹出一个消息窗口并暂停,直到用户按下关闭。由于我们正在进行压力测试并且无法禁用这些消息,因此此刻会导致很多问题,因此有人必须不断关注此过程。我一直试图提出一些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>

我需要它只是在后台持续运行,如果&#34;测试失败&#34;只执行SendKeys。窗口被选中/存在,并且能够在按下第二个按钮时停止循环。

我明显地弄错了(VBScript确实从上到下执行,不是吗?)并且我之前从未真正使用过这些功能,但似乎都设置正确。< / p>

我已经将@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}!\\.\root\cimv2")
        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

0 个答案:

没有答案