VBScript Send Key "

16,903

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.

Share:
16,903
Aaron B
Author by

Aaron B

Updated on June 04, 2022

Comments

  • Aaron B
    Aaron B about 2 years

    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?

  • Aaron B
    Aaron B over 7 years
    Nope still doesnt work...i guess because im using Windows 10 ? I tested a little bit and i never got app.activate to work...
  • user692942
    user692942 over 7 years
    @AaronB wait the code errors (tested) or you get "Activating Window Failed" returned?
  • user692942
    user692942 over 7 years
    @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.
  • Aaron B
    Aaron B over 7 years
    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...
  • user692942
    user692942 over 7 years
    @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.