-1

Is it possible to access a menu bar of an application with a batch script? I need to be able to write something that after opening an application, can select File>Save As.

tommyhawk
  • 51
  • 1
  • 5
  • 1
    Batch files can't natively interact with GUIs. What application are you trying to interact with? – SomethingDark Sep 17 '15 at 21:00
  • Maybe you might be interested in AutoHotKey (look it up in the web)... – aschipfl Sep 17 '15 at 21:04
  • Or the `SendKeys` function of vbscript if you're at work and can't install third-party software (but definitely try to use AutoHotKey if you can; SendKeys is notoriously unreliable). – SomethingDark Sep 18 '15 at 00:32

1 Answers1

0

Sample script that open a file in Notepad and sending keystrokes to: 1. Write some text. 2. Save the file. 3. Close the Notepad application. (3 times in a row)

/!\ Be aware, the batch-script will send the keystrokes to the application you have the focus./!\

@if (@CodeSection == @Batch) @then
@echo off
set "pr=_somefile.txt"
if NOT exist %pr% COPY NUL %pr%

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

for /L %%i in (1,1,3) do ( 
    rem Start notepad
    start "" notepad %pr%
    %SendKeys% "^{END}{ENTER}Hello, world! {(} started 1 line after the end of file {)}{ENTER}"
    %SendKeys% "Typing on notepad few lines{ENTER}"
    %SendKeys% "Saving the file{ENTER}^s"
    %SendKeys% "closing notepad in 1 sec !{ENTER}{ENTER}...............................{ENTER}"
    @timeout /T 1 /nobreak >NUL
    %SendKeys% "^s"
    %SendKeys% "^s%%{F4}"
    @timeout /T 1 /nobreak >NUL
)
goto :EOF

@end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Some references to read.
https://msdn.microsoft.com/en-us/library/aa266279(v=vs.60).aspx
http://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx
http://ss64.com/vb/sendkeys.html

Credit: https://stackoverflow.com/a/17050135

Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27