1
votes

I have 2 google chrome profiles, 1 for work, 1 for personal use. There are times when I have both of those profiles opened at the same time, and I want my ahk script to only run when a specific profile window is in focus. So I looked up some example, and made a test script like below:

IfWinActive, ahk_exe chrome.exe 
    {
        sPat = chrome.exe.*--profile-directory="Profile 4"
        for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process  where Name = 'chrome.exe'") 
            {
                If RegExMatch(process.Commandline,sPat)
                {
                    MouseMove, 500, 500
                    Return
                }
                Else
                {
                    Return
                }
            }
    }
Else 
    {
        Return
    }

But that doesn't work. The script can't detect the profile, and just skips straight to line 13. Where did I go wrong? Is there any way to fix it?

2
I can't look into it right now, but lemme ask some questions whose answers might be useful later on: 1. Are you going to manipulate something other than the webpage after detecting the user profile? (I know you probably are, but it doesn't hurt to ask) 2. Do you use TamperMonkey or ViolentMonkey and would you be okay to rely on them? Perhaps this can be accomplished without them, but if all else fails, they might help (like, for example, changing the window title).gyohza
Just a guess, but what if you replace sPat = chrome.exe.*--profile-directory="Profile 4" with sPat = "chrome\.exe.*--profile-directory=""Profile 4"""Martheen
gyohza: 1) Yes, there are several things I would like to do, such as go to a specific tab, copying highlighted info to an excel sheet, activate certain extension, switching youtube track, etc. 2) I have no idea what either of them are. I'm just a casual office guy, not some one with coding experience :(. Martheen: Thanks, I'll test it real quick. Brbnguyen long
@Martheen no dice, sorry man :(nguyen long
Have you verified by displaying all the process.CommandLine and test it against sPat on regex testing tools? Oh, also display the sPat inside message box, it might be interpreted differentlyMartheen

2 Answers

2
votes

Please, check, if it is work for you

/**
 * 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. - https://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)
}
1
votes

Based on previous answer here the extracted part to get the chrome profile of a chrome window:

Chrome_GetProfile(hwnd:=""){
If !hwnd
    hwnd := WinActive("A")

    title := Acc_ObjectFromWindow(hwnd).accName
    RegExMatch(title, "^.+Google Chrome . .*?([^(]+[^)]).?$", match)
    return match1
}

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)
}