Bloodied
Bloodied

Reputation: 975

How to write passed variables using sendkeys

Brief explanation: trying to use sendkeys to type two variables/keys from the output of a batch file.

I keep getting syntax or out of subscript errors.

Two variables are passed from a batch script, via:

cscript //nologo sendKeys.vbs !config! !arguments!
rem Yes, both variables are defined, and delayed expansion in enabled.

My attempts to put them in sendkeys doesn't seem to work. I'm not familiar with VBS...

VBScript:

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad.exe", 9             ' Start notepad in order to test my failure.
WScript.Sleep 500                         ' Give notepad time to load

WshShell.SendKeys & WshShell.Arguments(0) ' write passed !config! variable
WshShell.SendKeys "{TAB}"                 ' tab key
WshShell.SendKeys & WshShell.Arguments(1) ' write passed !arguments! variable
WshShell.SendKeys "{ENTER}"               ' enter key

Upvotes: 1

Views: 1322

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

The .Arguments belong to the WScript object, not the Shell. & is the concatenation operator, it makes no sense between a method name and the first/only argument. So use

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad.exe", 9             ' Start notepad in order to test my failure.
WScript.Sleep 500                         ' Give notepad time to load

WshShell.SendKeys WScript.Arguments(0)    ' write passed !config! variable
WshShell.SendKeys "{TAB}"                 ' tab key
WshShell.SendKeys WScript.Arguments(1)    ' write passed !arguments! variable
WshShell.SendKeys "{ENTER}"               ' enter key

or - better - think about a way to solve your real world problem without SendKeys.

Upvotes: 2

Related Questions