【问题标题】:vbscript to check if a window is open using wildcardvbscript 使用通配符检查窗口是否打开
【发布时间】:2016-09-12 17:10:26
【问题描述】:

我想检查是否在 vbscript 中使用通配符打开了一个窗口。我能够找到下面的代码:

Set oShell = CreateObject("WScript.Shell") 
If oShell.AppActivate("Untitled - Notepad") Then
   WScript.Sleep 500
End If

但我想在窗口标题上使用通配符。我尝试使用 * 和 % 但它不起作用。任何帮助表示赞赏。

If oShell.AppActivate("*Notepad*") Then

更新家伙..我能够找到一个解决方案,但如果有人可以简化它,它仍然是开放的。谢谢。

Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks

isFound = False
For i = 1 to 5
    For Each Task in Tasks
      checkVal = 0
      If Task.Visible Then
        checkVal = inStr(UCase(Task.name), UCase("outlook"))
        If checkVal <> 0 Then
            isFound = True
            Exit For
        End If
      End If
    Next
    If isFound = True Then
        Exit For
    End If
    WScript.Sleep 1000
Next

Word.Quit
msgbox ("Is the Window Found? - " & isFound)

【问题讨论】:

  • 这不是一个可以用来获取它的本地安装库。有一些库/包装器可以让您实现这一目标,但如果不获取/编码额外的库,您将无法从原始 VBScript 实现这一目标。您可以尝试使用带有 shell 的 TASKLIST 执行标准输出捕获,例如 tasklist /v | find /i "在这里搜索字符串"
  • 另外...您可以使用正则表达式从该任务列表输出中捕获窗口标题字段。 (?:.*)(?:\d:\d\d:\d\d\s)(.*)
  • 嗨史蒂夫 - 我试图找到一个使用正则表达式的解决方案,但幸运的是我能够找到一些解决方案,我已经更新了我上面的问题。顺便说一句,我就是这样做的,我得到了任务列表上的所有名称并在其上循环以使用 inStr 函数找到我正在寻找的字符串。
  • 有一种比创建 word 文档更快的方法......请参阅下面的答案。
  • AppActivate 已经有一个隐含的通配符。 In determining which application to activate, the specified title is compared to the title string of each running application. If no exact match exists, any application whose title string begins with title is activated. If an application still cannot be found, any application whose title string ends with title is activated. If more than one instance of the application named by title exists, one instance is arbitrarily activated.

标签: vbscript


【解决方案1】:

查看此方法与创建 word 文档。该方法需要安装 office 的依赖项。这仅使用本机 Windows 库。

第一个函数:findwindowtitle 执行 Tasklist 命令以枚举和过滤标题列表。然后触发正则表达式解析器以将您的字符串与任务列表中的剩余值进行匹配。

第二个功能:matchtitle 然后它继续使用将您的通配符转换为 [a-zA-Z0-9.- ] 的正则表达式,它是包含空格的字母数字。 Windows 文件需要通配符和允许的字符才能工作。

MysearchString = "*Notepad"
processtitle = findwindowtitle(MysearchString)
wscript.echo "My search found window title: '" & processtitle & "'"
'do something with processtitle 


function findwindowtitle(srchstr)
    filtersrchstr = replace(srchstr, "*", "")
    strcommand = "tasklist /v | find /i """ & filtersrchstr & """"
    cmdout = CreateObject("Wscript.Shell").Exec("cmd /c """ & strcommand & " 2>&1 """).stdout.readall
    wscript.sleep 500
    findwindowtitle = matchtitle(srchstr, cmdout)
End Function

Function matchtitle(srchstr, input)
    matchtitle = false
    if instr(1, srchstr, "*", 1) <> 0 Then 
        filtersrchstr = replace(srchstr, "*", "")
        filterstrpatt = replace(srchstr, "*", "[a-zA-Z0-9\.- ]*")
    end if
    Set regex = CreateObject("VBScript.RegExp")
    regex.MultiLine = True
    regex.Global = True
    regex.IgnoreCase = True
    regex.Pattern = "(?:.*)(?:\d\d?\d?:\d\d:\d\d\s)(\b" & filterstrpatt & "\b)"
    Set matches = regex.Execute(input)
    for m = 0 to matches.count - 1
        Set SubMatches = matches.item(m).SubMatches
        for i = 0 to (Submatches.count - 1)
            if instr(1, Submatches.item(i), filtersrchstr, 1) <> 0 then matchtitle = Submatches.item(i)
        Next
    Next
    if (matchtitle = false) then
        wscript.echo "Could not find process with title matching, '" & srchstr & "'"
        wscript.quit
    end if
End Function

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多