4

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.

  • 1
    That's because there aren't any. – SomethingDark Nov 14 '18 at 12:28
  • you can try with [sendKeys.bat](https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/sendKeys.bat) - to only bring an app to a focus you can omit the send keys part and only point the window name - `call sendKeys.bat "WindowTitle" ""` – npocmaka Nov 14 '18 at 12:49
  • Powershell can do this, if that's acceptable: https://stackoverflow.com/questions/42566799/how-to-bring-focus-to-window-by-process-name – SomethingDark Nov 14 '18 at 13:00
  • https://stackoverflow.com/questions/52381791/wscript-shell-appactivate-doesnt-work-every-time/52403754#52403754 – CatCat Nov 15 '18 at 02:10

2 Answers2

2

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
gotwo
  • 663
  • 8
  • 16
1

    @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"); 
    }

haseakash
  • 31
  • 1
  • 6
  • Copy all of this code... save as a batch file.. Just replace VOR to ur exe name. – haseakash May 31 '21 at 21:09
  • @Matt Williamson : This is working script im using. This works perfectly. Its a hrbrid jscript batch file code. copy code as it is. save into .bat file. replace VOR.exe with ur application name like app.exe – haseakash Feb 17 '22 at 18:06