Simulate keypress in bash

Hello everybody,

I am using Windows 10 and cygwin/bash.

I need to write a script in bash which simulates opening of a program and then press some keys such as F5, ENTER and ALT+F4.

I have written a VBScript which does the job

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Program"
WshShell.SendKeys "{F5}"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 8000
WshShell.AppActivate "Program"
WshShell.SendKeys "%{F4}"

but I need to use bash because the file I need to open is on the network.

I have no clue how to that with bash, any help is appreciated.

Thanks. Supernono06

Help me out - why shouldn't windows be able to open a file on the network?

BASH is poorly suited to this, almost nothing in Windows is designed using the UNIX process model. Your bash script will be a thin wrapper around whatever external utility you find which allows you to generate arbitrary keypresses (and odds are, that utility will be flagged and deleted by your antivirus).

Why not vbscript if you're really on win10?

I have a batch file calling the vbscript, and when it is on shared directory I got
"CMD does not support UNC paths as current directories"

MS Windows has three kinds of directory paths which partly overlap and partly don't and almost no programs implement all of them.

BASH would be no help here, as in UNIX, there's only one kind of path and network filesystems are the responsibility of the operating system, not the shell.

MS fortunately made a few kludges to join different kinds of paths together when your app needs a specific kind.

You should be able to turn a shared folder into a drive letter CMD can use, via something like

net use Z: \\computer_name\share_name /PERSISTENT:YES

That might even be a permanent mapping, unless you do

net use Z: /delete
5 Likes

Thank you Corona688, I will try this. I also had in mind to use pushd/popd, probably net use does the same?

pushd/popd is just a wrapper made around cd, it doesn't possess the magical ability to convert a network path into a local one. Altering the universe like that is an operating system feature. You have to ask Windows to do what you want.

I did not manage yet to get the net use function working but I got some progress with that script:

 
pushd \\files\share\path
start C:\"Program Files (x86)"\Program\bin\program.exe
wscript "Y:\path\script.vbs"
popd
 

Y is the first free letter on the server.
The program is opened, but the vbscript (see above) is not executed. It starts only when I close the program� Any clue on what happens?

To repeat: pushd is not capable of doing that. You really are barking up the wrong tree with this one. BASH is not a network browser.

Try net use. That is one of the only things capable of doing what you want.

I got it Corona688 and in my case it works perfectly and does what I need. Of course not with bash, but with a .bat file:

 
pushd \\files\share\path
start C:\"Program Files (x86)"\Program\bin\program.exe
sleep 3
wscript.exe Y:\path\script.vbs
sleep 3
popd
1 Like