Is it possible to put a command into a batch file that will enter text into a specified window and hit the enter key as if the user was interacting with it?
I know of a handy little exe for command prompt called "keystuff" that is capable of changing windows and inserting text, but i cant find where it has the capability to specify which window to change to since it just utilizes Alt+Tab to change the window.
You can, sort of -- but not entirely reliably, and not with pure Batch. You can use the
Wscript.Shell
COM object both for itsAppActivate
andSendKeys
methods.AppActivate
will let you change the focus to another window either by its PID or its title. When focusing by window title, the match is first attempted by full title, then title beginning, then title ending, in that order. So, for example, if you wanted to send keystrokes to Firefox, you could justshellObj.AppActivate('Firefox')
(because Firefox tabs end in "Mozilla Firefox").Unfortunately, there's no easy way to specify what part of the window gets the focus. If you want to
SendKeys
to the URL bar but the Search bar was last focused before the window was last blurred, then keys get sent to the Search bar. There's no way around that unless you simulate keyboard navigation (something like Ctrl+F to focus the Find-in-page, then Tab four times to focus the URL bar). It can get a little messy.Caveats aside, here's an example Batch + Jscript hybrid script (save it with a .bat extension) demonstrating both the
AppActivate
and theSendKeys
methods. See theSendKeys
TechNet article for a cheat sheet of symbols and their meanings (such as~
sending Enter).Using this script, if you enter
... you'll focus Firefox, send Ctrl+F to open or focus find-in-page, Backspace to erase any existing search string, Tab to the URL bar, navigate to Google, then close the find-in-page footer bar.
See? I told you. Messy. But doable.