VBScript 发送键 [英] VBScript Send Key "

查看:39
本文介绍了VBScript 发送键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

代码:

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

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

我该如何预防?

解决方案

这里的问题有两个;

  1. 字符串未正确转义,因为

    检查这个的方法是在继续脚本之前从 AppActivate() 返回一个布尔值,以确保它成功.

    选项显式Dim WshShell:设置 WshShell = WScript.CreateObject("WScript.Shell")Dim IsWindowActive: IsWindowActive = WshShell.AppActivate("Steam 社区 :: [GER] Aaron :: Abonnierte Objekte - Opera")如果 IsWindowActive 那么WshShell.SendKeys "^{2}"WScript.Sleep 5000WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"WshShell.SendKeys "{ENTER}"WScript.Sleep 5000WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"WshShell.SendKeys "{ENTER}"WScript.Sleep 5000WshShell.SendKeys "jQuery (""[id^='UnsubscribeItemBtn']"").children().trigger('click'); setTimeout(function(){location.reload();},500);"WshShell.SendKeys "{ENTER}"别的WScript.Echo激活窗口失败"万一

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

    激活窗口失败


    为什么 AppActivate() 返回 False?

    至于找出 AppActivate() 返回 False 的原因,我建议您查看

    答:WshShell.AppActivate 在简单的 vbs 脚本中似乎不起作用.

I have a VBScript to unsubscribe all Steam Workshop-Objects

Code:

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}"

And in Line 7 (Symbol 29) it has to send a " but the Script thinks it has to end the SendKey-Command there...

How can I prevent that?

解决方案

The issue here is two fold;

  1. The strings are not correctly escaped as @Scott has pointed out (Revision 1), but their latest edit isn't the way to fix that both "" and Chr(34) are equivalent and personally I'd stick with escaping by doubling the quotes.

  2. Invalid procedure or argument at Line 7 Symbol 1 (Code: 800a0005)

    is likely caused by SendKeys() when AppActivate() isn't set to the correct window.

    Notice what happens in VBSEdit while running the script, the SendKeys() injects into the script causing the runtime error.

    The way to check this is to return a Boolean from AppActivate() before continuing the script to make sure it is successful.

    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
    

    Output (because I don't have that particular Window):

    Activating Window Failed
    


    Why does AppActivate() return False?

    As to working out why AppActivate() returns False I'd recommend reviewing

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

这篇关于VBScript 发送键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆