9

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.

Milla Well
  • 2,973
  • 3
  • 29
  • 47
  • did you get the answer to this problem ? lately I have been stuck with similar problem. any help would be highly appreciated. thanks. – aj8080 Jan 23 '15 at 18:51

3 Answers3

3

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
Yaron
  • 163
  • 1
  • 11
1

if i got it right, start /f yourapp.exe would start the application in foreground.

denolk
  • 751
  • 14
  • 24
  • 1
    It says, that "/f" is an unknown argument. I tried `start /MAX /WAIT "" "my.exe"` , but it has the same behavior as `my.exe`. Maybe I need a helper app for this problem? – Milla Well Nov 25 '11 at 12:09
  • 1
    @MillaWell: You likely do. Windows controls focusing based on the most recent user actions and doesn't seem to expose the functionality of forcing the focus to the user. However, the functionality *is* exposed to a programmer, so maybe someone has written a helper application dedicated specifically to focusing an arbitrary window/program. – Andriy M Nov 25 '11 at 13:11
0

Yaron answer will work but would prefer not to create a temp file to execute the script but to embed the code in the batch directly. Or to use jscript which is also part of windows script host and is easier for embedding into batch Here's a focusOn.batenter link description here that will set the focus on an application based on starting string of the title (will not create temp files which will make it a little bit faster):

@if (@X)==(@Y) @end /* JScript comment
    @echo off
    cscript //E:JScript //nologo "%~f0"  %*
    exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */

var ARGS=WScript.Arguments;

if (ARGS.Length < 1 ) {
     WScript.Echo("No window title passed");
     WScript.Quit(1);
}

var sh=new ActiveXObject("WScript.Shell");
if(!sh.AppActivate(ARGS.Item(0))){
    WScript.Echo("Cannot find an app with window name starting with: " + ARGS.Item(0));
}

Example usage:

call focusOn.bat Untitled

which should put on focus the notepad (it its title is still "Untitled - Notepad")

npocmaka
  • 51,748
  • 17
  • 123
  • 166