## Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

Get help with using AutoHotkey and its commands and hotkeys
johnqflorida
Posts: 5
Joined: 22 Oct 2019, 09:02

### Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

First post here, from a bit of a newbie to AHK, but to a long-term keyboard shortcut addict.

I have a trusty old Windows Scripting Host / vbscript program that uses Sendkeys to send commands to whatever window was active, before I activated the script. In spite of the risks of Sendkeys, it's worked well enough for me for years. It is/was activated by built-in Windows desktop shortcuts with a shortcut key defined, to run, force some sendkeys into the buffer, exit, and in effect, the sendkeys in the buffer were channeled into whatever application was active right before I called the script via hotkeys.

I'm having trouble activating the script via Autohotkeys instead of the built in desktop shortcut's "shortcut-key" combination.

Disclaimer/explanation: I fully understand the irony of wanting this older/cruder method of sending keys to an application when there's AHK that I could use instead. The main reason is my old script is capable of some pretty elaborate parsing of input commands, including repeat factors, infinite nested subcommands, numbering, random test data creation, all sorts of crazy things, then sending that to my underlying application. Again, I realize AHK could do many of these things too, the catch is my script is available today, whereas converting to AHK would take me weeks/months/years. (It's also compatible with any other computer too.)

Question 1: With the old method, using the built-in shortcut-keys functionality of Windows desktop shortcuts, it worked surprisingly well, in spite of the risks. (In over 15 years, it's never accidentally opened a command window and unexpectedly typed "format c:/{ENTER}", for example. My question is this: I'm able to activate the script just fine via AHK, but I can't make it as reliable in sending the keys specifically to whatever window was previously the active one. (I've tried Run, Runwait, Return, Exit, ExitApp, all the variations I can think of as an AHK beginner) (It is more reliable with some pauses built in, for example, my current debugging shows me the active window before/after in a msgbox command, and that makes it a little more reliable in typing into the correct window.) I guess playing with some pausing is next on my list.

Question 2: I can see where without rewriting the whole script, I could potentially capture the raw Sendkeys output of my script into AHK, and use AHK's superior window handling to relay my keys, perhaps with a few changes between native Sendkeys syntax vs. AHK's. That's almost clearly superior, I'm open to someone just telling me how to do that.

Or, any other ideas? Thanks in advance!

Edit: Why am I asking? My new computer at work isn't playing nice with reliably running my built in desktop shortcut keys. Also, it's always been on my todo list to start using the more robust features AHK would make possible.

[Mod edit: Topic moved from 'Scripts and Functions' to 'Ask For Help']
Getfree
Posts: 217
Joined: 12 Oct 2014, 18:00

### Re: Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

Is it something like this? cscript path\to\my\script.vbs

Also, can you show the AHK script you are using to run that .vbs script.
johnqflorida
Posts: 5
Joined: 22 Oct 2019, 09:02

### Re: Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

The command line, within the desktop shortcut's "Shortcut" tab, has 'D:\WindowsScripts\okey.vbs' in the Target and in the Start In text boxes, directly calling the vbs script rather than calling wscript and providing the script as a parameter. I'll try also calling wscript with the parameter in my AHK script too, though. (I'm simplifying a bit here, it's a work environment, my local drive is actually a network drive mapped to D, but in theory, it's just a file, whether network drive or local.

To clarify a bit more on what my vbscript does, it prompts for input, where I can type in something as simple as an abbreviation, or a lengthy formula that decodes abbreviations, handles nesting, loops, can differentiate between abbreviations, vs. coincidentally sending text that is also an abbreviation, generate test data, etc., and the output is the expanded results of the what I typed in, as a Sendkeys command. For example, I could type in "100,rfirstname,t,rlastname,t,raddress,e,h" while using Excel, and it would parse my homegrown syntax, generate and type into excel 100 rows of test data, where "rfirstname" generates a random first name, "t" expands into "{TAB}", and on and on until "e" for {ENTER}", "h" for "{HOME}", etc. It began many years ago just sending basic tab commands to a web page, but has grown into doing all sorts of other strange things.

And the behavior I'm having with AHK interpreting the hotkey as opposed to my old way, a desktop shortcut with a shortcut key defined, is that the output *might* be in the expected window (whatever was active when I initiated the command), but it might also send the keys into my Notepad where I'm editing my AHK script, or some other window instead. (And as I mentioned in the original post, although there are no guarantees with Sendkeys, it still worked surprisingly well and reliably almost all of the time.)

The AHK script, I've tried a few things, starts simple: It just contains as follows:

Code: Select all

^+!o::
run, "D:\WindowsScripts\WindowsScripts\okey.vbs", , , Outpid
gregster
Posts: 3230
Joined: 30 Sep 2013, 06:48

### Re: Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

Did you try to run the original shortcut (.lnk) to the script via AHK instead of the vbs script itself? I have no idea if it really matters in this case (partly because I don't know how your vbs scripts work and what they do exactly), but sometimes the working directory ("Start in") makes a difference. I admit, I don't see how this would influence the activating of Windows (or why a simple AHK run command would change the last active window). Still, it might be worth a try.

The run command has also a corresponding parameter (the second) that could be used - and that you don't seem to set, so far - if this is really the issue.

But I might be misunderstanding the whole problem...

Edit:
Perhaps try to throw in a WinActivate and a short sleep to explicitly activate the 'last found/last active window/most recently used' window once again before running the script, but

Code: Select all

^+!o::
WinActivate
Sleep, 100
run, "D:\WindowsScripts\WindowsScripts\okey.vbs", , , Outpid
return
And you should close a multi-line hotkey generally with a return. If no code follwows, it doesn't really matter. But if there is other code following (like other hotkeys, hotstrings or labeled subroutines in general), the code flow of execution would just fall through and execute code that you don't expect to be executed at this point.
johnqflorida
Posts: 5
Joined: 22 Oct 2019, 09:02

### Re: Calling a vbscript/WSH, that does old fashioned sendkeys, and getting out of the way

@greg, and again @Getfree, thanks for the replies and help.

I believe the problem is solved, and just as you all expected, the issue was in the vbscript rather than AHK.

Solution in the VBScript: As I've said repeatedly, sendkeys comes with a degree of risk and uncertainty. My script has been around for over 15 years, starting in Windows XP. The XP version worked in a straightforward manner, although strategically placed sleep commands helped. When I got a Windows 7 machine, it didn't work reliably, and the workaround that made it predictable was to also throw a popup that expired a second later in addition to the sleep before sending the keys. Fast forward to now, my new Windows 10 computer, the popup STILL makes it work when invoking by a windows based shortcut key, but when invoking via AHK, that extra popup caused problems (I suppose in a manner of speaking, AHK served as that extra popup to begin with?)

So, thanks for the help, and sorry for the false alarm.

I'm hooked on AHK though, I've always meant to start using it. It invokes the hotkey much faster (it seems immune to how Windows checks for shortcut keys, where Windows (basically) checks each open window to see if it has a hotkey bound to it, and either if you have lots of windows open (small delay per open app, but it adds up), or a window that isn't responding (3 second delay per non-responding app), you are at risk of slower invocation.) Plus all the more powerful features available!