WinActivate not activating gvim window

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ajkessel
Posts: 27
Joined: 12 May 2017, 07:51

WinActivate not activating gvim window

Post by ajkessel » 28 Jan 2023, 17:28

I have a very simple function that either executes and activates a program or activates the window if it's already open. It works fine with most commands, but for gvim.exe it never gains focus automatically -- I have to click on the new window. I've tried adding delay before WinWait with no success. Oddly, it does work as expected with several other processes, just not gvim.exe. What am I missing?

Code: Select all

RunActivate(x,y := "")
{
   if (y == "" ) {
      SplitPath(x,&y)
   }
   if WinExist(y) {
      WinActivate 
   } else {
      Run x,,,&id
      WinWait "ahk_pid " . id
      WinActivate "ahk_pid " . id
   }
}

User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: WinActivate not activated gvim window

Post by mikeyww » 28 Jan 2023, 17:52

A process name is actually not a :arrow: WinTitle. Have a look at the documentation.

Some programs do not return a PID with the Run command, or the PID may change.

Code: Select all

#Requires AutoHotkey v2.0
pf86 := EnvGet('ProgramFiles(x86)')
app  := pf86 "\Vim\vim90\gvim.exe"
If !FileExist(app)
 MsgBox 'File not found.`n`n' app, 'Error', 48

F3::runActivate(app), SoundBeep(1500)

runActivate(filePath, winTitle := "") {
 (!winTitle) && (SplitPath(filePath, &fn), winTitle := 'ahk_exe' fn)
 If WinExist(winTitle)
  WinActive() ? WinMinimize() : WinActivate()
 Else Run filePath
}

ajkessel
Posts: 27
Joined: 12 May 2017, 07:51

Re: WinActivate not activating gvim window

Post by ajkessel » 29 Jan 2023, 10:12

Thanks for setting me straight. I also just learned there is now a Vim 9.0!

Post Reply

Return to “Ask for Help (v2)”