birgersp
birgersp

Reputation: 4916

WinActivate on window by process name?

Simply put, I want Ctrl+Alt+T to activate the Windows Terminal window. Previously I've used this:

^!T::
    if WinExist("Windows PowerShell")
        WinActivate
    else
        Run, wt
Return

But this doesn't cut it anymore, because the Windows Terminal changes its title when I am using Posh Git.

So I need to activate the window on whatever window that has the process name "WindowsTerminal.exe".

I've tried this but for some reason it does not recognize the correct window:

^!T::
    if WinExist(ahk_exe "WindowsTerminal.exe")
        WinActivate
    else
        Run, wt
Return

enter image description here

Upvotes: 1

Views: 4186

Answers (2)

Spyre
Spyre

Reputation: 2344

Your syntax for invoking WinExist with the name of a process/ exe is incorrect

Instead of:

if WinExist(ahk_exe "WindowsTerminal.exe")

You need to also include the ahk_exe part of it within the quotation marks.

So like this:

if WinExist("ahk_exe WindowsTerminal.exe")

Final Code:

^!T::
    if WinExist("ahk_exe WindowsTerminal.exe")
        WinActivate
    else
        Run, wt
Return

Upvotes: 8

birgersp
birgersp

Reputation: 4916

Solution:

^!T::
    _WindowId = -1
    WinGet _Windows, List
    Loop %_Windows%
    {
        _Id := _Windows%A_Index%
        WinGet, _PName , ProcessName, ahk_id %_Id%
        if (_PName == "WindowsTerminal.exe")
        {
            _WindowId = %_Id%
            break
        }
    }
    if (_WindowId != -1)
    {
        WinActivate, ahk_id %_WindowId%
    } else
    {
        Run, wt
    }
Return

There's probably a shorter way to do this with AHK but I can't be bothered with that gross syntax anymore.

Upvotes: 0

Related Questions