【问题标题】:VBScript Send Key "VBScript 发送键"
【发布时间】:2017-01-12 19:40:18
【问题描述】:

我有一个 VBScript 可以取消订阅所有 Steam 创意工坊对象

代码:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Steam Community :: [GER] Aaron :: Abonnierte Objekte - Opera"
WshShell.AppActivate "Steam Community :: [GER] Aaron :: Abonnierte Objekte - Opera"
WshShell.AppActivate "Steam Community :: [GER] Aaron :: Abonnierte Objekte - Opera"
WshShell.SendKeys "^{2}"
WScript.Sleep 5000
WshShell.SendKeys "jQuery ("[id^='UnsubscribeItemBtn']").children().trigger('click'); setTimeout(function(){location.reload();},500);"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "jQuery ("[id^='UnsubscribeItemBtn']").children().trigger('click'); setTimeout(function(){location.reload();},500);"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "jQuery ("[id^='UnsubscribeItemBtn']").children().trigger('click'); setTimeout(function(){location.reload();},500);"
WshShell.SendKeys "{ENTER}"

在第 7 行(符号 29)中,它必须发送一个 " 但脚本认为它必须在那里结束 SendKey-Command...

我怎样才能防止这种情况发生?

【问题讨论】:

标签: vbscript sendkeys steam


【解决方案1】:

这里有两个问题;

  1. 字符串没有正确转义,因为@Scottpointed out (Revision 1),但their latest edit 不是解决""Chr(34) 等效的方法,我个人会坚持使用通过双引号进行转义。

  2. 第 7 行符号 1 处的过程或参数无效(代码:800a0005)

    AppActivate() 未设置为正确的窗口时,可能是由 SendKeys() 引起的。

    注意在运行脚本时 VBSEdit 中发生了什么,SendKeys() 注入到脚本中导致运行时错误。

    检查这一点的方法是从AppActivate() 返回一个布尔值,然后继续执行脚本以确保它成功。

    Option Explicit
    Dim WshShell: Set WshShell = WScript.CreateObject("WScript.Shell")
    Dim IsWindowActive: IsWindowActive = WshShell.AppActivate("Steam Community :: [GER] Aaron :: Abonnierte Objekte - Opera")
    If IsWindowActive Then
        WshShell.SendKeys "^{2}"
        WScript.Sleep 5000
        WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"
        WshShell.SendKeys "{ENTER}"
        WScript.Sleep 5000
        WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"
        WshShell.SendKeys "{ENTER}"
        WScript.Sleep 5000
        WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"
        WshShell.SendKeys "{ENTER}"
    Else
        WScript.Echo "Activating Window Failed"
    End If
    

    输出(因为我没有那个特定的窗口)

    Activating Window Failed
    


    为什么AppActivate() 返回False

    关于为什么AppActivate()返回False我建议回顾一下

    A: WshShell.AppActivate doesn't seem to work in simple vbs script.

【讨论】:

  • 不行还是不行...我猜是因为我使用的是 Windows 10?我测试了一下,我从来没有让 app.activate 工作......
  • @AaronB 等待代码错误 (tested) 或者你得到“激活窗口失败”返回?
  • @AaronB 我已经解释了为什么您会收到您在 Scott 的回答 (自从删除) 上发布的错误,我很确定我已经做到了。您现在的问题是让AppActivate() 返回True,这可能是由于 Window 的 WindowClass 请参阅A: WshShell.AppActivate doesn't seem to work in simple vbs script
  • 我激活窗口失败。我查看了您链接的问题,如果我理解我只需要放入 appactivate “opera.exe”?我不是编码专家...
  • @AaronB 这是我怀疑的。您需要研究该链接的答案,但传递可执行文件名称将不起作用,它是窗口标题,但如果失败则进程 ID (这是为进程生命周期分配的数值) 您可以通过使用 WMI 遵循 Bonds 方法获得。无论哪种方式,我都很确定您的初始查询已得到回答,如果您发现此答案或其他答案有用,请考虑留下投票,如果问题回答了您的初始问题,请考虑接受它。希望这对您有所帮助。
【解决方案2】:

我发现 sendkey 在某些键上进行“翻译”的艰难方式。请参阅https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx的表格

不幸的是,我没有找到将SendKey 置于“原始”模式以不进行解释的方法。所以我必须处理字符串以预先撤消翻译。由于我使用PowerShell,所以我使用接下来的两行:

${SendKeyStr2Type} = "${Str2Type}" -replace "([\\{\\}\\[\\]\\(\\)~+^%])",'{$1}'
$wshell.SendKeys("${SendKeyStr2Type}")

详情:-replace 后有 2 个字符串,正则表达式样式。第一个列出要转义的字符{}[]()~+^%。使用\\ 将它们转义为正则表达式。那是在[]-s 中选择其中一个。那是在()-s 中,用于下一个(替换)字符串。第二个字符串是$1,正则表达式输入找到的字符串(这里是单个字符)并在{}-s之间输入。

请记住,sendkey 发送 key-strokes。他们冒昧地在<shift><othercaracter> 使用了一些字符,以便能够键入没有其字符的键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2020-12-30
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多