Activate Chrome with specific profile

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Activate Chrome with specific profile

22 Aug 2017, 07:54

Hello.

If you run multiple Chrome windows with different profiles, and you need to activate the window with a specific profile, then this script is for you.
I recommend to use distinctive names for the profiles, for example, Work, Games, or Profile1, Profile2, etc

Special thanks to rbrtryn who created the original script to get the command line info of the running process.
All I did is to adapt it to my needs.

Code: Select all

ActivateChrome() {
var1 := "chrome.exe" ;process name
var2 := "00Default"    ;set the specific profile
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process  where Name = '" var1 "'") {
        Commandline := process.Commandline
        str := process.ProcessId              
        StringReplace Parameters, Commandline, % Process.ExecutablePath
        StringReplace Parameters, Parameters, ""
        IfInString, Parameters, %var2%
         {
           WinActivate ahk_pid %str%        
           Break
         }
    }    
}

I run it with no problems in Windows 10 Pro 64bit and AHK v1.1.26.01.
User avatar
tdalon
Posts: 44
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

Re: Activate Chrome with specific profile

18 Dec 2019, 02:22

Unfortunately from a Chrome window opened with a second profile the process.Commandline still contains the first opened Profile information.
At least for me with windows 10 and latest chrome Version 79.0.3945.79
Argimko
Posts: 3
Joined: 07 Mar 2016, 06:22

Re: Activate Chrome with specific profile

17 Jul 2020, 08:06

Please, check, if it is work for you

Code: Select all

/**
 * Description : AutoHotKey script bring specific Chrome Profile windows to the front.
 *             : While Chrome is active press Ctrl+1 ... Ctrl+5 to activate another profile.
 * Tested with : Chrome v84
 * Freelancer  : Art G. - www.upwork.com/freelancers/~017155e179702d4657
 * Version     : 2020-07-17
 */

#Warn
#NoEnv
#SingleInstance Force

SetBatchLines -1
SetTitleMatchMode 2

GroupAdd ChromeWindow, ahk_exe chrome.exe ahk_class Chrome_WidgetWin_1

#IfWinActive ahk_group ChromeWindow
    ^1:: ActivateProfile("Default")
    ^2:: ActivateProfile("Profile 2")
    ^3:: ActivateProfile("Profile 3")
    ^4:: ActivateProfile("Profile 4")
    ^5:: ActivateProfile("Profile 5")
#IfWinActive

ActivateProfile(profile) {
    static profiles := {}, hwnds := {}

    If (!profiles[profile]) {
        FileRead preferences, % SubStr(A_AppData, 1, -8) "\Local\Google\Chrome\User Data\" profile "\Preferences"
        profiles[profile] := {}

        RegExMatch(preferences, "O)""name"":\s*""(.+?)""", match)
        profiles[profile]["name"] := match[1]

        RegExMatch(preferences, "O)""given_name"":\s*""(.+?)""", match)
        profiles[profile]["given_name"] := match[1]
    }

    WinGet hwndList, List, ahk_group ChromeWindow
    Loop % hwndList {
        hwnd := hwndList%A_Index%
        If (!hwnds[hwnd]) {
            title := Acc_ObjectFromWindow(hwnd).accName
            RegExMatch(title, "O)^.+Google Chrome . .*?([^(]+[^)]).?$", match)
            hwnds[hwnd] := match[1]
        }

        If (hwnds[hwnd] == profiles[profile]["name"] || hwnds[hwnd] == profiles[profile]["given_name"]) {
            WinActivate ahk_id %hwnd%

            For hwnd In hwnds {
                If (!WinExist("ahk_id" hwnd))
                    hwnds.Delete(hwnd)
            }

            Return
        }
    }

    Run chrome.exe --profile-directory="%profile%"
}

Acc_Init() {
    static h := DllCall("LoadLibrary", Str,"oleacc", Ptr)
}

Acc_ObjectFromWindow(hwnd, objectId := 0) {
    static OBJID_NATIVEOM := 0xFFFFFFF0

    objectId &= 0xFFFFFFFF
    If (objectId == OBJID_NATIVEOM)
        riid := -VarSetCapacity(IID, 16) + NumPut(0x46000000000000C0, NumPut(0x0000000000020400, IID, "Int64"), "Int64")
    Else
        riid := -VarSetCapacity(IID, 16) + NumPut(0x719B3800AA000C81, NumPut(0x11CF3C3D618736E0, IID, "Int64"), "Int64")

    If (DllCall("oleacc\AccessibleObjectFromWindow", Ptr,hwnd, UInt,objectId, Ptr,riid, PtrP,pacc:=0) == 0)
        Return ComObject(9, pacc, 1), ObjAddRef(pacc)
}
grossermanitu
Posts: 18
Joined: 29 Mar 2016, 10:49

Re: Activate Chrome with specific profile

30 Oct 2020, 12:36

@Argimko

I tried to use your script but for me, nothing happens.

I just have to replace "ActivateProfile("Default")" "Default" and "Profile 2-5" with the profile names that appear under "Other people" in Chrome, correct? Or is there anything else that I need to adjust?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 126 guests