Reputation: 405
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.
Upvotes: 19
Views: 43476
Reputation: 2552
Here is a cmd one-liner with no external dependency. It is designed to start or bring-to-front Microsoft Edge, which has multiple processes and we have to find the correct one first (the one with a MainWindowHandle).
powershell -command "$edgeProcess = Get-Process -Name msedge -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 }; if ($edgeProcess) { (New-Object -ComObject wscript.shell).AppActivate($edgeProcess.Id) } else { Start-Process msedge }"
Upvotes: 0
Reputation: 3877
My solution (which I've also put in a gist here) 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.
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");
}
Upvotes: -1
Reputation: 202
Here is my solution in pure Windows API:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment( lib, "user32" )
#include "types.h"
i32 wmain(ui64 argc, wchar_t **argv)
{
if(argc != 2)
{
return 1;
}
// Find window by Class Name
HWND wnd = NULL;
ui64 tries = 100;
while(--tries != UI64_MAX)
{
wnd = FindWindow(argv[1], NULL);
if(wnd != NULL)
{
break;
}
Sleep(50);
}
if(wnd == NULL)
{
return 2;
}
AllowSetForegroundWindow(ASFW_ANY);
DWORD cur_thread = GetCurrentThreadId();
DWORD fg_pid = 0;
DWORD fg_thread = GetWindowThreadProcessId(wnd, &fg_pid);
AttachThreadInput(cur_thread, fg_thread, TRUE);
SetFocus(wnd);
SetForegroundWindow(wnd);
ShowWindow(wnd, SW_NORMAL);
AttachThreadInput(cur_thread, fg_thread, FALSE);
SetWindowPos(wnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
return 0;
}
Compile it and just call the compiled program with the argument of the window's class name. You can get window's class name with programs like WinSpy++ or similar. If you want it to search by title, I think you can easily modify it yourself. There is tons of info on how to find window handle by title.
Oh yes, it also restores window if it is currently minimised.
If it fails to find the window, it keeps trying up to 100 times with 50 ms intervals (so total 5 seconds), after that it gives up. It is useful if you are starting some program from CMD, and it takes a few seconds to start up, but you want it brought to focus immediately after its ready.
Upvotes: 0
Reputation: 19
Powershell -command "$wshell = New-Object -ComObject wscript.shell ; $wshell.AppActivate('App title')"
Upvotes: 1
Reputation: 57252
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.
Upvotes: 17
Reputation: 67216
This should work:
echo (new ActiveXObject("WScript.Shell")).AppActivate("app A"); > focus.js
cscript //nologo focus.js
del focus.js
Upvotes: 10