Script Center > Scripting Forums > The Official Scripting Guys Forum! > How can I ensure that the "SendKeys" method sends the keys to the appropriate window
Ask a questionAsk a question
 

AnswerHow can I ensure that the "SendKeys" method sends the keys to the appropriate window

  • Wednesday, November 04, 2009 6:33 AMKiranChaturvedula Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a FTP client that also has Console interface for automation. In order to use this I am using the below

    Set ccaFtpShellScript = WScript.CreateObject("WScript.Shell")
    ccaFtpShellScript.run "cmd /K CD /d C:\Program Files\WinSCP\" 'This  opens the FTP client in console window

    ccaFtpShellScript.SendKeys  "winscp.exe /console /script=FTPConnectAndGetXMLQA.txt"  ' This a console command prescribed 'by the client with FTP connection details specified in the text file FTPConnectAndGetXMLQA

    'To send these keys I need the console window to be the active window. If I click on any other applications window in the mean 'time the send keys method writes the command line to that application

    When I try to use "cmd /C" switch the result is the same. No errors but I do not know where is it being written.

    How can I tell windows to inactivate other windows force during this time so that no one or no application can create a new window?
    OR
    How can I activate a particular window before executing "SendKeys" method.

Answers

  • Wednesday, November 04, 2009 1:53 PMTom Lavedas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    First, why do you want to use Sendkeys to invoke the executable?  It can be executed directly using something like this ...

      Set oShell = CreateObject("WScript.Shell")
      oShell.Run  "C:\Program Files\WinSCP\winscp.exe /console /script=FTPConnectAndGetXMLQA.txt"

    Or if the working folder needs to be changed first ...

      Set oShell = CreateObject("WScript.Shell")
      oShell.curentdirectory = "C:\Program Files\WinSCP"
      oShell.Run  "winscp.exe /console /script=FTPConnectAndGetXMLQA.txt", 1, True ' shows window and waits

    Finally, if you insist on using Sendkeys, try using the AppActivate function something like this example ...

    Set oShell = CreateObject("WScript.Shell")
    oShell.run "%comspec% /k", 1 , false
    sTitle = oShell.ExpandEnvironmentStrings("%COMSPEC%")
    Do until oShell.AppActivate(sTitle) :wsh.sleep 50 : Loop ' wait to open
    if oShell.AppActivate(sTitle) then oShell.Sendkeys "DIR *.txt /b{enter}" ' send the command
    wsh.sleep 2000 ' pause
    if oShell.AppActivate(sTitle) then oShell.Sendkeys "exit{enter}" ' close the example window

    Tom Lavedas

All Replies

  • Wednesday, November 04, 2009 1:53 PMTom Lavedas Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    First, why do you want to use Sendkeys to invoke the executable?  It can be executed directly using something like this ...

      Set oShell = CreateObject("WScript.Shell")
      oShell.Run  "C:\Program Files\WinSCP\winscp.exe /console /script=FTPConnectAndGetXMLQA.txt"

    Or if the working folder needs to be changed first ...

      Set oShell = CreateObject("WScript.Shell")
      oShell.curentdirectory = "C:\Program Files\WinSCP"
      oShell.Run  "winscp.exe /console /script=FTPConnectAndGetXMLQA.txt", 1, True ' shows window and waits

    Finally, if you insist on using Sendkeys, try using the AppActivate function something like this example ...

    Set oShell = CreateObject("WScript.Shell")
    oShell.run "%comspec% /k", 1 , false
    sTitle = oShell.ExpandEnvironmentStrings("%COMSPEC%")
    Do until oShell.AppActivate(sTitle) :wsh.sleep 50 : Loop ' wait to open
    if oShell.AppActivate(sTitle) then oShell.Sendkeys "DIR *.txt /b{enter}" ' send the command
    wsh.sleep 2000 ' pause
    if oShell.AppActivate(sTitle) then oShell.Sendkeys "exit{enter}" ' close the example window

    Tom Lavedas