Skip to content

Instantly share code, notes, and snippets.

@hubisan
Last active March 26, 2024 16:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubisan/5981dcf2a8560df9b434dd3b7d8e357b to your computer and use it in GitHub Desktop.
Save hubisan/5981dcf2a8560df9b434dd3b7d8e357b to your computer and use it in GitHub Desktop.
AHK Open Chrome with Profile #ahk

AHK Open Chrome with Profile

This Autohotkey function lets you open or activate Chrome with a specific profile. In addition in returns its hwnd (ahk_id).

Examples

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.

Function Documentation

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.

Remarks

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:

  1. If chrome.exe is active it activates the tray instead (to not identify an already open Chrome window with the wrong profile later on).
  2. 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.
  3. Then waits for the window with process name chrome.exe to get active.
  4. 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.
  5. In the end it returns the hwnd of the window.
; ------------------------------------
; Open or activate Chrome with a profile.
; ------------------------------------
; If Chrome with the profile specified doesn't exist yet it will open it.
; If Chrome with the profile specified is already open it will activate it.
;
; Args:
; 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. If it was not possible to get the hwnd it returns 0.
;
; Example:
; chrome_hwnd_default := open_chrome_with_profile("Default")
; chrome_hwnd_profile_1 := open_chrome_with_profile("Profile 1")
; WinActivate, % "ahk_id " . chrome_hwnd_default
; Sleep 1000
; WinActivate, % "ahk_id " . chrome_hwnd_profile_1
; open_chrome_with_profile("Profile 1", "www.google.com")
; ------------------------------------
open_chrome_with_profile(profile_name:="Default", url:="", wait_ms_to_close:=200, wait_s_to_activate:=5) {
static chrome_profiles := {}
chrome_path := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
; Check if profile is stored already and the hwnd still exists if no url is
; specified.
if !url {
profile_hwnd := chrome_profiles[profile_name]
if (profile_hwnd && WinExist("ahk_id " . profile_hwnd)) {
WinActivate, % "ahk_id " . profile_hwnd
return profile_hwnd
}
}
; Build the target.
run_target := """" . chrome_path """"
run_target .= "--profile-directory=""" . profile_name . """"
WinGet, current_process, ProcessName, A
if (current_process = "chrome.exe") {
; Focus the tray to be able to wait for chrome.exe to get active later on.
WinActivate, % "ahk_class Shell_TrayWnd"
WinWaitActive, % "ahk_class Shell_TrayWnd", , 1
}
if url {
; If an url is specified just run the profile with that url and wait for
; chrome to be active.
run_target .= " " . url
Run, % run_target
WinWaitActive, % "ahk_exe chrome.exe", , wait_for
if ErrorLevel {
return 0
} else {
WinGet, chrome_hwnd, ID, A
chrome_profiles[profile_name] := chrome_hwnd
return chrome_hwnd
}
} else {
; The seconds loops is only needed in case chrome with that profile
; didn't exist before and it the session doesn't have any other tabs as
; this code will close the new tab opened and therefore also close the
; chrome instance. In that case a 2nd iteration is needed which opens
; chrome with a new tab.
Loop, 2 {
if (A_Index = 1) {
Run, % run_target . " example.com"
} else {
Run, % run_target
}
WinWaitActive, % "ahk_exe chrome.exe", , % wait_s_to_activate
if ErrorLevel {
return 0
} else {
; Get the hwnd of the window and close the fake tab.
WinGet, chrome_hwnd, ID, A
; Only close the tab on the first iteration.
if (A_Index = 1) {
SendInput, ^w
Sleep, % wait_ms_to_close
}
; Check if the window handle still exists. If chrome with that
; profile was not open before closing the tab closes chrome as well
; and the hwnd will not exist anymore.
if WinExist("ahk_id " . chrome_hwnd) {
chrome_profiles[profile_name] := chrome_hwnd
return chrome_hwnd
}
}
}
}
}
@hubisan
Copy link
Author

hubisan commented Oct 1, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment