2

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?

user692942
  • 16,398
  • 7
  • 76
  • 175
AaronVB
  • 37
  • 1
  • 2
  • 6
  • 4
    Possible duplicate of [Adding quotes to a string in VBScript](http://stackoverflow.com/questions/2942554/adding-quotes-to-a-string-in-vbscript) – scrappedcola Jan 12 '17 at 19:46

2 Answers2

2

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.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Nope still doesnt work...i guess because im using Windows 10 ? I tested a little bit and i never got app.activate to work... – AaronVB Jan 14 '17 at 21:20
  • @AaronB wait the code errors *(tested)* or you get "Activating Window Failed" returned? – user692942 Jan 14 '17 at 21:36
  • 1
    @AaronB I've explained why you were getting the error you posted on Scott's answer *(since removed)*, which I'm pretty sure I've done. Your problem now is getting `AppActivate()` to return `True` which is likely due to the WindowClass of the Window see [A: WshShell.AppActivate doesn't seem to work in simple vbs script](http://stackoverflow.com/a/23184667/692942). – user692942 Jan 15 '17 at 11:31
  • I got Activating Window Failed. I looked the Question you linked and if i understood i just need to put in the appactivate "opera.exe" ? Im not a Coding pro... – AaronVB Jan 15 '17 at 12:11
  • 1
    @AaronB which is what I suspected. You need to study that linked answer, but passing the executable name won't work, it's either the Window Title but if that fails the Process Id *(which is a numerical value assigned for the life of the process)* which you can get by following Bonds approach using WMI. Either way I'm pretty sure your initial query has been answered, if you find this or other answers useful consider leaving an up-vote and if question answered your initial question consider accepting it. Hope this has been helpful. – user692942 Jan 15 '17 at 12:18
0

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.

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
CBee
  • 49
  • 2