none
Windows Script Host 5.7 (XP SP3) and AppActivate - It's not working the same anymore!?

    Question

  • Let's start with the following code on a patched to the hilt Windows XP SP2 (32-bit) machine:
    Patched to the hilt XP SP2 means all patches applied EXCEPT for SP3

    Set objShell = WScript.CreateObject ("WScript.Shell")  
    '* Set System Performance Optipons (best performance)  
       objShell.Run "control sysdm.cpl" 
         
          Do While objShell.AppActivate("System Properties") = FALSE  
             WScript.Sleep 500 '* just loop sleep until window is available  
          Loop 
            
          objShell.SendKeys "{home}"     '* ensure general tab focus (default focus on invoke)  
          objShell.SendKeys "{right 3}"  '* set focus on Advanced tab  
          objShell.SendKeys "%s"         '* Performance section, settings button  
            
             Do While objShell.AppActivate("Performance Options") = FALSE  
                WScript.Sleep 500  
             Loop 
               
             objShell.SendKeys "%p" '* adjust for best performance  
             objShell.SendKeys "%a" '* apply changes  
             WScript.Sleep 5000     '* sleep 5 sec to allow settings apply  
               
          objShell.SendKeys "{enter}" '* Perfomance Options window, OK button  
            
       objShell.SendKeys "{enter}" '* System Properties, OK button 

    This code works fine until you apply SP3 and Windows ScriptHost 5.7 is installed. Instead of HOME and RIGHT RIGHT RIGHT getting me to the Advanced tab, I am looking at activity on the OK and Cancel buttons which makes no sense because even with another window open and using ALT+TAB to switch back and forth...once System Properties (sysdm.cpl) is called into focus the General tab has focus and the only way to change that by keypress is with a TAB or SHIFT+TAB which I am clearly not sending.

    I have been using the Do Until object.AppActivate("WindowTitle") = TRUE and/or Do While object.AppActivate("WindowTitle") = FALSE loop to do something until the window appears for years now. Unless someone can point me to some fancy API's that are exposed to VBScript without need for installation of anything and really accomplish the same thing as the UI settings for the Recycle Bin, Start Menu taskbar, and the Best Performance option set in the code above I need to use this. FYI recycle bin uses some sort of serialization that is easy enough to reproduce via script, but until the UI is invoked settings are hit and miss - do script work and invoke UI = 100% settings success. Since I have to invoke the UI anyway I just do it VIA sendkeys!

    The code below corrects the problem after SP3 installs WSH 5.7, but then I am left HOPING that the window in front is indeed the window to pass the keys to!

    Set objShell = WScript.CreateObject ("WScript.Shell")  
    '* Set System Performance Optipons (best performance)  
       objShell.Run "control sysdm.cpl" 
          WScript.Sleep 5000  
          objShell.SendKeys "{right 3}" 
          WScript.Sleep 1000  
          objShell.SendKeys "%s" 
             WScript.Sleep 5000  
             objShell.SendKeys "%p" 
             WScript.Sleep 1000  
             objShell.SendKeys "%a" 
             WScript.Sleep 5000  
          objShell.SendKeys "{enter}" 
       WScript.Sleep 1000  
       objShell.SendKeys "{enter}" 

    AutoIT is not an option - we use and can only use what the OS comes with so PowerShell is not an option until it gets included with the O/S via SP or we upgrade to Vista or greater. We really try to limit what we install on the system and there are several reasons for that that are beyond this post.

    This problem occurs for ANY window title that does not appear in the Windows Task Manager tasks listed under the Applications tab. It used to work and we have been testing SP3 with our automation and this is the only reason we caught this. Our user scripts started failing after the SP3 update was applied.
    Wednesday, December 24, 2008 11:02 PM

Answers

  • Hey...I think the problem is that the script host seems to be loosing the window focus so i put the appActivate method in a for loop for each keystroke...this worked for me on XP SP3...try modifying to your requirements....this was also a very useful article

    http://articles.techrepublic.com.com/5100-10878_11-1056137.html

    Hope this helps

    Cheers

    Matt :)

    Option Explicit  
    Dim wshShell  
    On Error Resume Next 
       Set wshShell = WScript.CreateObject("WScript.Shell")  
       If Err.Number <> 0 Then 
          Wscript.Quit  
       End If 
       SetBestPerformance  
       If Err.Number <> 0 Then 
          MsgBox Err.Number & " " & Err.Description  
       End If 
    On Error Goto 0  
    Function SetBestPerformance  
       Dim i, spKeys, poKeys  
       spKeys  = Array("{TAB}","{TAB}","{Right}","{Right}","{Right}","%s")  
       poKeys  = Array("%p","%a")  
       wshShell.Run "control sysdm.cpl" 
       Do While Not wshShell.AppActivate("System Properties")  
          Wscript.Sleep 500  
       Loop 
       For i = 0 To UBound(spKeys)  
          wshShell.AppActivate("System Properties")  
          wshShell.SendKeys spKeys(i)  
       Next 
       Do While Not wshShell.AppActivate("Performance Options")  
          Wscript.Sleep 500  
       Loop 
       For i = 0 To UBound(poKeys)  
          wshShell.AppActivate("Performance Options")  
          wshShell.SendKeys poKeys(i)  
       Next 
       Wscript.Sleep 5000  
       wshShell.SendKeys "{enter}" 
       wshShell.SendKeys "{enter}" 
    End Function 
    Thursday, December 25, 2008 5:41 AM

All replies

  • Hey...your right this code doesn't work with XP SP3

    http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx

    At the bottom of the page..."SendKeys() has been disabled on Vista/Server 2008, due to security concerns"...don't know if thats true but it wouldn't surprise me if certain functions have been changed with the latest version of windows script host. If I figure out another way to do the same thing then i'll post it.
    Thursday, December 25, 2008 4:01 AM
  • Hey...I think the problem is that the script host seems to be loosing the window focus so i put the appActivate method in a for loop for each keystroke...this worked for me on XP SP3...try modifying to your requirements....this was also a very useful article

    http://articles.techrepublic.com.com/5100-10878_11-1056137.html

    Hope this helps

    Cheers

    Matt :)

    Option Explicit  
    Dim wshShell  
    On Error Resume Next 
       Set wshShell = WScript.CreateObject("WScript.Shell")  
       If Err.Number <> 0 Then 
          Wscript.Quit  
       End If 
       SetBestPerformance  
       If Err.Number <> 0 Then 
          MsgBox Err.Number & " " & Err.Description  
       End If 
    On Error Goto 0  
    Function SetBestPerformance  
       Dim i, spKeys, poKeys  
       spKeys  = Array("{TAB}","{TAB}","{Right}","{Right}","{Right}","%s")  
       poKeys  = Array("%p","%a")  
       wshShell.Run "control sysdm.cpl" 
       Do While Not wshShell.AppActivate("System Properties")  
          Wscript.Sleep 500  
       Loop 
       For i = 0 To UBound(spKeys)  
          wshShell.AppActivate("System Properties")  
          wshShell.SendKeys spKeys(i)  
       Next 
       Do While Not wshShell.AppActivate("Performance Options")  
          Wscript.Sleep 500  
       Loop 
       For i = 0 To UBound(poKeys)  
          wshShell.AppActivate("Performance Options")  
          wshShell.SendKeys poKeys(i)  
       Next 
       Wscript.Sleep 5000  
       wshShell.SendKeys "{enter}" 
       wshShell.SendKeys "{enter}" 
    End Function 
    Thursday, December 25, 2008 5:41 AM
  • Insane! And here I thought I was pretty creative with some of my scripting prowess! Slipping the pre-defined keystrokes into an array. I tried a lot of different things, but this was not one of them!

    It appears to work.

    I did see that community content regarding SendKeys being disabled in Vista/2008, but I have not found anything from MS specifically to substantiate that claim. I did see that WSH5.7 can/may change the default script host from WScript.exe to CScript.exe, but nothing about send keys or AppActivate getting whacked in any way.

    Thanks Matthew and thanks for the Dictionary example in my other question!
    Daniel
    Friday, December 26, 2008 11:52 PM