Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus a batch-started application

I am running a sequence of applications from a batch script and I want to make sure, that the opened program will always be in focus.
We need to ensure this because it's an experimental setup and we want to minimise such hassles as having to change focus to a fullscreen window.

This problem already occurred infrequently when the earlier program exits and for a moment the desktop is visible and the user clicks on some icon on the desktop, and right after that, the next program in the sequence is being processed, the new window is not in focus.

The problem has become much more frequent now that I hid the command window from view.

Any way to force focus for the next program in the sequence, be it a batch command, some settings for the OS (we're on Win XP) or a helper app could be helpful.

like image 822
Milla Well Avatar asked Nov 25 '11 09:11

Milla Well


People also ask

How do I close a program with batch?

Type taskkill /IM your-program-name. your-program-extension /T /F and then hit ↵ Enter . Repeat this command for as many programs as you want! When finished, type exit on the last line and hit ↵ Enter .


1 Answers

If you want to focus another program you can do this.

call focus WindowTitle
exit /b

:focus
setlocal EnableDelayedExpansion 

    if ["%~1"] equ [""] (
        echo Please give the window's title.
        exit /b
    )

    set pr=%~1
    set pr=!pr:"=!

    echo CreateObject("wscript.shell").appactivate "!pr!" > "%tmp%\focus.vbs"
    call "%tmp%\focus.vbs"
    del "%tmp%\focus.vbs"

goto :eof 
endlocal 

I am using vbscript to focus the application. You need to pass the window's title, not the window's name (whatever.bat). To make sure you get the right window focused you can set its title. example:

title WindowTitle
like image 91
Yaron Avatar answered Sep 30 '22 01:09

Yaron