title: autohotkey how to activate last opened window that changes its pid
body: <p>I'm trying to setup a hotkey to always launch a new WindowsTerminal window and give it focus.  Even if there is an existing window, I always want it to create a new window.  I'm able to get the new window to run, but I'm having difficulty making it the Active window.</p>
  <p>Capturing the pid during the <code>Run</code> command and using <code>ahk_pid</code> doesn't seem to work as the pid changes between launch and the active window that appears (msgbox debugging shows one pid, Window Spy shows a different pid).  Using <code>WinWait, ahk_exe WindowsTerminal.exe</code> seems to return right away grabbing a pre-existing window.</p>
  <pre><code>#t::
  Run, wt.exe, , , TermPid
  
  ;; TermPid seems to change between the launch and the final window that appears
  WinWait, ahk_pid %TermPid%, , 3
  if ErrorLevel {
    MsgBox, WinWait timed out... %TermPid%
    return
  } else {
    WinActivate
  }
  
  ;; problems when there are other already existing WindowsTerminal.exe windows
  ;WinWait, ahk_exe WindowsTerminal.exe, , 3
  ;if ErrorLevel {
  ;  MsgBox, WinWait timed out...
  ;  return
  ;} else {
  ;  WinActivate
  ;}
  
  return
  </code></pre>