3

I'm looking for a command that can bring a window into focus. The command has to work in a cygwin terminal, but I imagine this command will be built into Windows, so it's worth saying I'm using Windows 10. I figure this command exists, but I've been unable to find it from googling.

I'm basically asking this but with cygwin instead of Ubuntu: Bash command to focus a specific window

These vbs scripts look promising, but when I execute them in cygwin, they neither error, nor change focus. CMD command in Windows to switch to an already open application It's as if AppActivate is a noop.

I'm running wscript.exe switch.vbs "App Title" with this script:

WScript.CreateObject("WScript.Shell").AppActivate(WScript.Arguments.Item(0))

It's probably wise to state why I want this functionality: I've made it so that when my tests run from my cygwin commandline, it notifies me the way Windows 10 does with any normal event: with an event bubble. The command I'm using to create that notification (notifu) returns different exit codes depending on what happened to the bubble. It'll return an exit code of 3 if I click on it, but whatever window was focused remains in focus. Instead, I want to click on the bubble and focus my cygwin terminal.

Daniel Kaplan
  • 579
  • 8
  • 23

1 Answers1

2

How do I set focus to a Window?

You can use nircmd win focus ....

Example:

nircmd win focus title "bash"

where bash is the title of my Cygwin Terminal window.

Syntax

nircmd win [action] [find] [window to find] [Additional Parameters]

There are different ways to specify the windows to find:

handle: Finds the desired window by specifying the handle of the window in [window to find] parameter.
id: Finds the desired child window by specifying the id of the child window. Useful only for child windows.
class: Finds the desired window by specifying the class name of the window in [window to find] parameter.
title: Finds the desired window by specifying the exact title of the window in [window to find] parameter.
stitle: Finds the desired window by specifying the first few characters of the window in [window to find] parameter.
ititle: Finds the desired window by specifying a sequence of characters that exists in the window title.
alltop: All top windows.
alltopnodesktop: All top windows, except of desktop and tray windows.
active: The current active window.
foreground: The window in foreground.
desktop: The desktop window
process:Finds the desired window by specifying process ID (for example: /3412) or process name (for example: firefox.exe).

Source Nircmd Win

Go to NirCmd - Windows command line tool to download nircmd (download link is at the bottom of the page).

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • Just like with `AppActivate`, running this command fails to do anything and there is no error given. Here's the command I wrote: `nircmd win focus ititle "Notepad"` I put Nircmd inside my `~/bin` which is on my path, and I've tried running this in a cygwin terminal (mintty) with and without admin privileges. It doesn't make a difference. It doesn't make a difference if I run it in powershell either. – Daniel Kaplan Jul 25 '21 at 18:01
  • Okay, I figured it out. What I really wanted was `activate`, not `focus`. I'm not sure if I got the terminology wrong or not here: I've never heard anybody use the phrase "activate the `x` window". I still don't know why the vbs script doesn't work. – Daniel Kaplan Jul 25 '21 at 18:15