Locking focus on batch file - batch-file

I have a simple batch file in windows that I run on startup that presents the user with a menu to start certain applications. However by default, whenever I open one of these applications the focus goes to that window and off of the batch file. Is there a way to maintain, or re-divert focus onto the batch window?
Thanks
EDIT: Got it to do what I wanted. Used foxidrives suggestion to start them minimized but they were still taking focus. So I made a short VBScript to make the cmd window the active window after each call and the combination of the two worked. Thanks!

There is no command to steal the focus. As Bill_Stewart posted, that would be a dangerous feature that grants the program too much power over the user. You can however start the applications minimized (they will still be the active window), and then build and call a VBScript to make your batch window the active window:
start "" /MIN "application.exe"
cscript /nologo myVBScript.vbs
myVBScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "myBatchFile"
WScript.Sleep 2000
WshShell.AppActiavte "myBatchFile"
I've read that several people have had trouble with AppActivate on Windows 7. It was not functioning for me, and instead of bringing my target window to the foreground it just blinked in the task bar. Calling it a second time however, for some reason, brought it to the foreground and made it the active window, which is what I wanted. Hope this helps anybody else with a similar issue.

you can't lock the focus to your command prompt window. But what you could do is to set the TopMost flag of the command prompt window. There is a Win32 function called SetWindowPos which does that. Maybe there are some ready to use command line tools around which are doing this for you. Or, if you have visual studio installed, try to compile this one here: How to set a console application window to be the top most window (C#)?

If you use the start command with the /min switch then the application will be minimised and the batch file should remain visible:
#echo off
pause
echo launching notepad
start "" /min notepad
echo notepad is active
pause

Related

WScript.Shell AppActivate doesn't work every time

I have a few scripts to help running a program in CMD. The first script that starts the sequence is run on startup. In the process, I'm running a VBScript with which I'm trying to focus on the CMD and make it fullscreen. However, sometimes it works and sometimes it doesn't and I'm not changing anything, just restarting the computer over and over again. It doesn't always focus on the cmd window so the problem should be with the AppActivate method.
Here is the code for the first script that runs on startup:
#echo off
TITLE masterBat
START "Master" /B /W myApp.bat
shutdown.exe /s /t 00
Here is the code for myApp.bat:
#echo off
cscript fullscreen.vbs
cd <path>
CMD /C <command>
exit
Here is the code for fullscreen.vbs:
Set ws = WScript.CreateObject("WScript.Shell")
ws.AppActivate "masterBat - myApp.bat"
ws.SendKeys "{F11}"
Set ws = Nothing
The title in the AppActivate call is the title on the cmd when I'm trying to make it fullscreen. I've also tried adding some WScript.Sleep calls in the script but it doesn't seem to help.
Wscript.sleep will make it worse. You don't want to sleep.
There are rules. Your program MUST obey one of these rules to be allowed to set the foreground window.
From https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
•The process is the foreground process.
•The process was started by the foreground process.
•The process received the last input event.
•There is no foreground process.
•The process is being debugged.
•The foreground process is not a Modern Application or the Start
Screen.
•The foreground is not locked (see LockSetForegroundWindow).
•The foreground lock time-out has expired (see
SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
•No menus are active.
An application cannot force a window to the foreground while the user
is working with another window. Instead, Windows flashes the taskbar
button of the window to notify the user.
What this means is you have two seconds (FOREGROUNDLOCKTIMEOUT) from when your script starts to set another window if your script doesn't have a user interface (as it can't be the foreground window). A message box as the first thing you do will comply (till something else happens). Also Wscript has a message box that times out - WshShell.Popup.
This is to prevent slow starting programs, when the user has got bored and is now working in another program, from then stealing the focus.
Preventive comment Normally people want to argue with me about this. Complain to Microsoft not me. And you can't argue with rules. You can just comply with them.

batch file from task scheduler win10, not working as excpected

I have an old exe I wrote that I think would make a nice screen saver. The problem is it needs the enter key pressed to start it.
I wrote a simple batch file to accomplish this:
start cells.exe
timeout /t 1
nircmd sendkey enter press
this works great when I run it from windows explorer... but when I schedule it to run from the task scheduler on idle, the .exe runs, but the key stroke never makes it through. I have tried running the task at the highest privileges, but still the same behavior. The .exe runs but doesn't receive the key stroke. Any suggestions would be very much appreciated.
Because Task scheduler is awkward about running batch files you need to put in an intermediary vbs file and run that from Task scheduler instead.
{Assuming your batch file is called StartCells.bat}
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "[FullPath]\StartCells.bat" & Chr(34), 2
Set WshShell = Nothing
This runs your batch file in a minimised window that nevertheless keeps the focus so that the intended operation of the nircmd line is not disrupted.
Denis
I got it to work by editing the registry to point the the .exe I complied from the .bat for my screen saver. I still don't understand why it wont run from task scheduler, but I understand now the this is the wrong forum for the question. Thanks for looking.

Positioning the batch window

I am trying to find a way to adjust the window that opens while you are running a batch file WITHOUT using 3rd party programs. I have adjusted the size and color of it, but would like to move the window to the top left corner of the screen. This also has to be self contained in the batch file it self(not right-clicking on title bar and changing the settings of window position that way).
So for example, I have a batch file that opens, you answer the questions it asks then it opens the required program to finish the tasks(such as logging you into putty). But then, I would like the batch window to move to top left(already adjusted size and color to make it more noticeable) so it's easier for the user to click on it to see the "codes" I added to it to use in putty.
This is going to be distributed to multiple users and we can't add 3rd party programs and I don't want to have to explain to everyone how to adjust how the window opens, I just want it to always open top left(no matter how the computers are set up).
Lastly, all PCs are running Windows 7 and thanks in advance for the help!
Run the batch file from Start-Run:
set title=COMMAND PROMPT
reg add "hkcu\console\%title%" /v WindowPosition /t REG_DWORD /d "0" /f
start "%title%" cmd "%~f0 %title%"
exit /b

How to generate key strokes from a batch file?

I want to create a batch file to do the following:
Start selenium server(webdriver-manager start)
Run Protractor tests(protractor conf.js)
Stop Selenium server()
This needs 2 different command prompts since webdriver-manager start will keep running and simultaneously the tests need to be executed
I have achieved the following so far. I have created a .bat file with the following contents:
start runTests.cmd
webdriver-manager start
Ctrl-C(**DOES NOT WORK**)
However, I am not able to figure out a way to shutdown the Selenium server(which is achieved by pressing Ctrl+C on the same window on which the webdriver-manager start command is executed)
You can generate keystrokes using VB Script, which can be called from a batch file.
I followed this post: http://www.w7forums.com/threads/f5-key-via-batch-file.18046/, substituting {F5} with ^{C} for Ctrl+C. So my file looks like:
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^{C}"
You need to save this file with a ".vbs" extension.
I also found from this answer VBScript - switching focus to window using AppActivate how to set the focus to another window (which you know the title of). Doing this first, you can direct your keystroke to the appropriate window, so:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Untitled - Notepad"
objShell.SendKeys "^{C}"
I experimented with an empty instance of Notepad, so you'll need to change the window title string appropriately. (Ctrl+C doesn't do anything in Notepad, but Alt+F4 closes it, so I used "%{F4}" to test it.)
Then you can simply call the VBS file from your batch file to run it.

Answer a question in a popup window

I have a batch file that I want to use to exicute a .exe file. The exe runs but it pops up a windows needing some information. How can I file in the popup with the correct information? The servername.domain is the server address that needs to be put in the popup. I thought that this was the syntax to populate the popup but its not working. Any help would be great.
SetupCodeGroup.exe servername.domain
i have found that you can not do what you want to do with a batch file. when i ran into a simaler problem i used vbscript sendkeys method. if you google that you should be able to do what you want.
and for your case i would have a batch file as a bass to run the vbscrit program you write to fill in the info in the .exe
somthing like this for the batch
(if you do copy and past this be sure to take out the comments (//) )
#echo off
set reapet=100 // just setting varibles to be used in the delay loop
set con=1
set time=0
start (your .exe) //starts your EXE
:begin
set /a time=%time%+%con%
if %time%==%reapet% goto:startvb // this makes a delay so that we can be sure your
goto:begin // computer fully started the .exe :)
:startvb
cls
start your (vbscript) // this starts your vbscript
cls
echo Done // tells you the batch file is done
pause
i hope this helps.

Resources