AaronVB
AaronVB

Reputation: 37

VBScript Send Key "

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?

Upvotes: 2

Views: 25382

Answers (2)

CBee
CBee

Reputation: 49

I found out the hard way that sendkey does its 'translations' on some keys. See the table at https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx

Unfortunately, I did not find a way to put SendKey in 'raw' mode to not interpret. So I have to process the string to pre-undo the translation. Since I use PowerShell, I use the next 2 lines:

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

Details: After -replace there are 2 strings, regex style. The first lists the characters to escape {}[]()~+^%. They are escaped for regex using \\. That is in []-s to get a selection of one of them. That is in ()-s to be used in the next (replace) string. The second string is $1, the regex way to type the found string (single character here) and type it between {}-s.

Keep in mind, sendkey sends key-strokes. They took the liberty to use some characters at <shift><othercaracter> to be able to type keys that do not have their character.

Upvotes: 0

user692942
user692942

Reputation: 16680

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.

    Screenshot

    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.

Upvotes: 2

Related Questions