Using command line/batch to switch to/focus on app

Viewed 31637

Is there an command i can use on cmd to switch to/focus on selected app window?

Lets say i have three apps working: A, B, C and currently i m focused on B. Is there a command (not shortcut) to switch to/focus on app A?

Maybe can i do it with batch?

Im working on windows.

3 Answers

My solution in this gist is very much based on @npocmaka's answer, but it gives a more complete and exact answer to the OPs specific question (I had to incorporate a couple more references to get to this): it activates and restores the named app if it is already running, or starts it if not.

I'll repeat the code here for completeness - replace the image name of the app and the path required to start it as you need:

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

@echo off 
setlocal
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq Fork.exe" ^| find /I "Fork.exe"') do set pid=%%i
if "%pid%" == "" (
    %localappdata%\Fork\Fork.exe
) else (
    cscript //E:JScript //nologo "%~f0" "%~nx0" "%pid%"
)
exit /b %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"); 
}
Related