Option Explicit ' -------------------------------------------------- ' Setup ' -------------------------------------------------- ' Essential Variables Dim ApplicationTitle Dim ApplicationPath Dim ApplicationStartupDelay Dim ApplicationSendkeysDelay Dim SendkeysTextFile Dim SendkeysText Dim Matches Dim Regex Dim DelayMatches Dim DelayRegex ' Sendkeys Escaping Mechanism Detection - matches WSH Sendkeys escapes Set Regex = New RegExp Regex.Pattern = "^(\~|\+.|\^.|\%.|{[^}]+}|{}})" ' Add Delay Feature to Sendkeys Syntax Set DelayRegex = New RegExp DelayRegex.Pattern = "^{DELAY [0-9]+}$" ' Helper Stuff Dim Wsh Dim Fso Dim Tso Dim i Dim s Dim buf ' Arguments and Configuration If (WScript.Arguments.Count < 3 ) Then PrintUsage() SendkeysTextFile = WScript.Arguments.Item(0) ApplicationTitle = WScript.Arguments.Item(1) ApplicationPath = WScript.Arguments.Item(2) ' Wrap in quotes for passing to Shell again ApplicationPath = """" & ApplicationPath & """" If (WScript.Arguments.Count > 3 ) Then ApplicationStartupDelay = WScript.Arguments.Item(3) Else ApplicationStartupDelay = 1000 End If If (WScript.Arguments.Count > 4 ) Then ApplicationSendkeysDelay = WScript.Arguments.Item(4) Else ApplicationSendkeysDelay = 100 End If ' Files Set Wsh = CreateObject("WScript.Shell") Set Fso = CreateObject("Scripting.FileSystemObject") Set Tso = Fso.OpenTextFile(SendkeysTextFile) SendkeysText = Tso.ReadAll() ' -------------------------------------------------- ' Action ' -------------------------------------------------- If (not (Wsh.AppActivate(ApplicationTitle))) Then Wsh.Run(ApplicationPath) Wscript.Sleep(ApplicationStartupDelay) if (not (Wsh.AppActivate(ApplicationTitle))) Then WScript.Echo("Error: Could not activate application" & _ ApplicationTitle & " using path " & _ ApplicationPath) WScript.Quit(1) End If End if For i = 1 To (Len(SendkeysText)) s = Mid(SendkeysText, i, 1) set Matches = Regex.Execute(Mid(SendkeysText, _ i,Len(SendkeysText) - i + 1)) If (Matches.Count > 0) Then buf = Matches.Item(0).Value i = i + Matches.Item(0).Length - 1 ' Wscript.Echo("===> " & buf & " <===") & vbCrLf If (DelayRegex.Test(buf)) Then set DelayMatches = DelayRegex.Execute(buf) Wscript.Sleep(Mid(DelayMatches.Item(0).Value, _ Len("{DELAY ") + 1, _ (Len(DelayMatches.Item(0).Value) _ - (Len("{DELAY ") + 1)))) Else Wsh.Sendkeys(buf) End If Else Wsh.Sendkeys(s) End If Wscript.Sleep(ApplicationSendkeysDelay) Next Wscript.Quit(0) ' -------------------------------------------------- ' subs ' -------------------------------------------------- Sub PrintUsage Dim UsageText UsageText = vbCrLf & _ "Usage: sendkeys " & _ "[startup-delay] [sendkeys-delay]" & vbCrLf & _ vbCrLf & _ "Arguments and Options:" & vbCrLf & _ vbCrLf & _ " cmd-file: file containing keys to send " & vbCrLf & _ " app-title: window title of target application window" & vbCrLf & _ " app-path: path to target application" & vbCrLf & _ " startup-delay: delay after application startup (in milliseconds, default 1000)" & vbCrLf & _ " sendkeys-delay: delay between keys (in milliseconds, default 100)" & vbCrLf & _ vbCrLf WScript.Echo(UsageText) WScript.Quit(1) End Sub