This Autohotkey function lets you open or activate Chrome with a specific profile. In addition in returns its hwnd (ahk_id).
You might have to adapt
chrome_path
inside the function.
-
Activate Chrome with profile "Profile 1":
open_chrome_with_profile("Profile 1")
If Chrome with that profile doesn't exist it will open it.
-
Open an url in Chrome with Profile "Default":
open_chrome_with_profile("Default", "www.google.com")
-
Activate Chrome with profile "Default" and store its hwnd (ahk_id):
chrome_hwnd_default := open_chrome_with_profile("Default") WinGetTitle, win_title, % "ahk_id " . chrome_hwnd_default MsgBox, % win_title
The function returns the hwnd of the chrome window with the profile specified.
Arguments:
- profile_name (str, optional)
The name of the profile like "Default" or "Profile 1". Defaults to "Default". - url (str, optional)
If specified open a new tab in chrome with the profile specified. Defaults to "" which means that it either actives the already existing chrome or open it without visiting an url. - wait_ms_to_close (int, optional)
After sending Ctrl-w it needs to wait for the tab or the app to close before proceeding. If Chrome only opens for a split second and doesn't get activated you have to increase this. Defaults to 200 ms. - wait_s_to_activate (int, optional)
Maximal time in seconds it waits for Chrome to get active. Defaults to 5 seconds. Depending on your system you might increase this. This is just to make sure the scripts doesn't wait forever in any case.
Returns:
(hwnd) The window handle of chrome window of the profile specified. This can be used as ahk_id.
Was not able to find a solution to target a Chrome window with a specific profile. That's why I came up with this hack.
The hack works as follows:
- If
chrome.exe
is active it activates the tray instead (to not identify an already open Chrome window with the wrong profile later on). - Runs Chrome with the profile (as command line argument) and
a) the url specified or
b) a fake url (www.example.com)
The url is needed to not open a 2nd instance if Chrome with that profile is already open. - Then waits for the window with process name
chrome.exe
to get active. - If it used a fake url
a) it close the fake tab opened.
b) If closing the tab also closes the window it will run Chrome again without an url. - In the end it returns the hwnd of the window.
Added
wait_ms_to_close
argument which lets you increase the sleeping time after sending Ctrl-w to really wait for the tab and especially if there is only one tab for the window. Increased the default to 200 ms as 50 ms seemed to be not long enough.