Activating multiple instances of Chrome trough WinActivate and PID

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Tup
Posts: 62
Joined: 25 Jun 2015, 13:10

Activating multiple instances of Chrome trough WinActivate and PID

Post by Tup » 28 Jan 2023, 10:50

Hello to all of you.
I try to add some functinality to a larger script of mine.
I am new to using PID's, but Forum questions/answers and the user manual helped me halfway.
I can't seem to find a solution for the WinActivate part though.

This is the simplified version of my script:
Ctrl_Win_F11 runs two instances of Chrome, at the same time I retrieve the corresponding PID's.

Later on I want to activate one of these Chrome windows (they are on two monitors), making use of the retrieved PID's.
Ctrl_Win_F12 Messagebox shows the correct PID, but WinActivate doesn't activate the corresponding Chrome window.

No idea.

Any help or tip is highly appreciated.
Thx,
Tup

Code: Select all

#SingleInstance force
#NoEnv

Chrome1PID := 0
Chrome2PID := 0


^#F11::
Run, C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe https://calendar.google.com/calendar/u/0/r/week,,, Chrome1PID
	Sleep, 1000
  
;just to check:
MsgBox, % Chrome1PID 

Run, C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe --new-window https://web.whatsapp.com/,,, Chrome2PID
Sleep, 1000
;just to check:
MsgBox, % Chrome2PID

return


^#F12::
;just to check:
MsgBox, % Chrome2PID
WinActivate, ahk_pid %Chrome2PID%,

return

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

Re: Activating multiple instances of Chrome trough WinActivate and PID

Post by mikeyww » 28 Jan 2023, 11:55

Hi,

I would use the HWND instead.

Code: Select all

#Requires AutoHotkey v1.1.33

url      := "http://www.autohotkey.com/"
browser  := "chrome.exe"
winTitle := "ahk_class Chrome_WidgetWin_1 ahk_exe " browser
wait     := 200

^#F11::
Run %browser% %url%
WinWaitActive % winTitle
Sleep wait
WinMaximize
Sleep wait
hWnd1 := WinExist()
WinMinimize
Sleep wait
Run %browser% --new-window %url%
WinWaitActive % winTitle
Sleep wait
WinMaximize
hWnd2 := WinExist()
WinMinimize
SoundBeep 1500
Return

^#F12::
WinActivate ahk_id %hWnd1%
SoundBeep 1500
Return

Code: Select all

#Requires AutoHotkey v2.0

url      := "http://www.autohotkey.com/"
browser  := "chrome.exe"
winTitle := "ahk_class Chrome_WidgetWin_1 ahk_exe " browser

^#F12::WinActivate(hWnd1), SoundBeep(1500)
^#F11:: {
 SetWinDelay 200
 Run browser ' ' url
 WinWaitActive winTitle
 WinMaximize
 Global hWnd1 := WinExist()
 WinMinimize
 Run browser ' --new-window ' url
 WinWaitActive winTitle
 WinMaximize
 Global hWnd2 := WinExist()
 WinMinimize
 SoundBeep 1500
}

Tup
Posts: 62
Joined: 25 Jun 2015, 13:10

Re: Activating multiple instances of Chrome trough WinActivate and PID

Post by Tup » 28 Jan 2023, 12:10

Thank You Mikey, I need a little time to implement your code (I am turtle-level in scripting)
My AHK version is 1.1.22.06. (Is that a problem?)

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

Re: Activating multiple instances of Chrome trough WinActivate and PID

Post by mikeyww » 28 Jan 2023, 15:44

My usual answer: try it and see! If you remove the first line, you will probably be OK.

Tup
Posts: 62
Joined: 25 Jun 2015, 13:10

Re: Activating multiple instances of Chrome trough WinActivate and PID

Post by Tup » 28 Jan 2023, 17:35

Thank you very much, it works fine now! :bravo:

Post Reply

Return to “Ask for Help (v1)”