Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 866
regularny Avatar asked Mar 14 '16 13:03

regularny


People also ask

Which command do you use to change the focus of the Command Prompt to a different directory?

To change your command prompt to focus on a different drive, just type the drive letter followed by a colon. From there you can use the “cd” command to access other directories on that different drive.

What is %% A?

%%a are special variables created by the for command to represent the current loop item or a token of a current line. for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered.

How do I use command line switches?

Use a switch once by adding it to the Run command In the Run dialog box, type a quotation mark, enter the full path for the app's .exe file, and then type another quotation mark. Alternatively, click Browse to locate and select the file. In this case, the quotation marks are supplied automatically.

What is P in command?

The P (process) command submits the command constructed in the primary output buffer to the TCL command processor.


3 Answers

This should work:

echo (new ActiveXObject("WScript.Shell")).AppActivate("app A"); > focus.js
cscript //nologo focus.js
del focus.js
like image 97
Aacini Avatar answered Oct 09 '22 11:10

Aacini


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.

like image 23
npocmaka Avatar answered Oct 09 '22 09:10

npocmaka


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"); 
}
like image 32
MikeBeaton Avatar answered Oct 09 '22 09:10

MikeBeaton