13

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.

regularny
  • 337
  • 1
  • 2
  • 11

4 Answers4

14

try with sendKeys.bat :

call sendkeys.bat "Title A" ""

The first argument is the beginning of the title you want to switch to (if it is Some Title you can use only Some if it is unique enough). Second argument are empty double quotes. In general the script is used to send keys to a particular window. But if you left the second parameter empty (like "") it will not send any keys.

Community
  • 1
  • 1
npocmaka
  • 53,069
  • 18
  • 138
  • 177
  • Thank, this script help me a lot! – regularny Mar 15 '16 at 12:00
  • @Garric - there's a code that activates the window - `if (sh.AppActivate(title)){` , also the OP accepted the answer . The answer itself explain the rest. – npocmaka May 28 '20 at 10:50
  • 1
    Thanks. I was looking for a similar function – Garric May 29 '20 at 14:28
  • This .bat file seems not working if the window is minimized... is there a way to solve this? – shelper May 19 '21 at 20:32
  • @shelper there is , but requires a call to another script - check this: https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/c/windowMode.bat - check the usage with `-h` – npocmaka May 20 '21 at 07:23
  • @npocmaka interestingly, `sendkeys` finds window with title "title1 title2" by either "title1" or "title2" but `windowMode` can only find window using the beginning word "title1". `sendkeys` always bring window to front but cannot find minimized window, and `windowMode` can find minimized window but does not bring window to front. Would be nice if they can merge some features. – shelper May 20 '21 at 15:01
  • @shelper - Isn't an option to use both tools in your script? – npocmaka May 21 '21 at 04:09
  • @npocmaka it seems impossible even by both tools if I want to find a minimized window "title1 title2" by "title2" and bring it to front ... – shelper May 21 '21 at 14:41
  • @shelper do you know the PID of the window? Another option is to use [TASKLIST](https://ss64.com/nt/tasklist.html) and filter the process with FIND and then to use windowMode.bat with the PID. – npocmaka May 21 '21 at 14:56
  • @npocmaka humm, PID changes if i restart the program... TASKLIST may work, thanks for the suggestion... – shelper May 21 '21 at 15:03
8

This should work:

echo (new ActiveXObject("WScript.Shell")).AppActivate("app A"); > focus.js
cscript //nologo focus.js
del focus.js
Aacini
  • 61,954
  • 12
  • 66
  • 99
  • this used to work for me fine then one day I starte getting errors: `Access is denied. Input Error: Can not find script file "C:\focus.js". Could Not Find C:\focus.js` – Kalamalka Kid Jun 03 '20 at 06:04
1

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"); 
}
MikeBeaton
  • 2,693
  • 2
  • 29
  • 42
0

enter image description here@MikeBeaton, I use your code to work on outlook. However, the batch file does not close itself if it opens a new outlook. Please advise where should be modified. The batch file remains opened but blank.blank batch

@if (@X)==(@Y) @end /* JScript comment 
@echo off 
setlocal
for /f "tokens=2" %%i in ('tasklist /FI "IMAGENAME eq 
Outlook.exe" ^| find /I "Outlook.exe"') do set pid=%%i
if "%pid%" == "" (
 "%systemdrive%\Program Files (x86)\Microsoft 
Office\Office12\OUTLOOK.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"); 
 }
Willie Cheng
  • 6,537
  • 11
  • 43
  • 63
VB88
  • 9
  • 4