Matt Williamson
Matt Williamson

Reputation: 59

Switch Window Focus using a Batch File/CMD

I have a Windows application that when launched with the Task Scheduler on start up does not have focus (i.e. I cannot execute keyboard commands in the application until I click the application into focus). Focus is on the Desktop.

The application is started through Task Scheduler as it required Highest Privileges to launch (bypassing UAC prompt).

There only seems to be third party applications available which support this to switch to the application. I have tried launching a command with a batch file to switch the focus, which has a delayed start, but I haven't been able to find any Windows batch file commands which can switch the focus.

Upvotes: 4

Views: 12600

Answers (2)

haseakash
haseakash

Reputation: 77


    @if (@X)==(@Y) @end /* JScript comment 

    @echo off 
    setlocal
    for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq VOR.exe" ^| find /I "VOR.exe"') do set pid=%%i
    if "%pid%" == "" (
        %localappdata%\VOR\VOR.exe
    ) else (
        cscript //E:JScript //nologo "%~f0" "%~nx0" "%pid%"
    )
    exit %errorlevel% 
    endlocal

    @if (@X)==(@Y) @end JScript comment */ 

    var sh=new ActiveXObject("WScript.Shell"); 
    if (sh.AppActivate(WScript.Arguments.Item(1)) == 0) {
        sh.SendKeys("% r"); 
    }

Upvotes: 1

gotwo
gotwo

Reputation: 870

It's possible to switch into the foreground by using of a Windows script. For that it is required to provide a file which will be created by a batch command also. After processing of the script, the file will be deleted. The commands are the following:

echo new ActiveXObject("WScript.Shell").AppActivate("Firefox"); > tmp.js
cscript //nologo tmp.js & del tmp.js

Upvotes: 2

Related Questions