Autohotkey – Want to launch program and bring it’s window to the front

autohotkeyscript

I want an Autohotkey script which launches a new instance of Chrome and brings that new window to the forefront (active window).

My script is:

#c::
Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -start-maximized --new-window www.google.com
WinActivate, Google ; Window title is "Google"

What happens is this: if, prior to activating the hotkey there is any arbitrary window in focus, the browser instance launches in the background which is not what I want. If however the desktop or Taskbar has focus then my new Chrome window comes to the forefront as I want it to.

Best Answer

Most likely WInactivate does not work because you need to add SetTitleMatchMode, 2 at the top of your script. To test if WinActivate really works use this:

SetTitleMatchMode, 2
#c::
Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -start-maximized --new-window www.google.com
WinWait, google ;Give Chrome a chance to start before testing
IfWinExist, google
{
    WinActivate, google ; Window title is "Google"
    MessageBox, OK
}
Return

If you do NOT see the OK message, your Winactivate still waits for the correct title. Once you are sure this works, you can reduce is back to:

SetTitleMatchMode, 2
#c::
Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -start-maximized --new-window www.google.com
WinWait, google ;Give Chrome a chance to start before testing
WinActivate, google ; Window title is "Google"
Return
Related Question