Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking focus on 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!

like image 814
user3768274 Avatar asked Mar 08 '26 09:03

user3768274


2 Answers

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.

like image 101
user3768274 Avatar answered Mar 11 '26 09:03

user3768274


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
like image 35
foxidrive Avatar answered Mar 11 '26 09:03

foxidrive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!