Activating a different chrome profile user

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bxgchoi
Posts: 16
Joined: 28 Oct 2020, 13:48

Activating a different chrome profile user

20 Sep 2021, 11:47

Hi!

I currently have this simple script

Code: Select all

^2::

winactivate, ahk_exe chrome.exe
Sleep, 500
MouseMove, 148, 346
MouseClick, Left

it works well for now, however, i am going to start using multiple chrome profiles. how can i make sure that the script activates the correct window or profile? Is there a different name for the other profiles? Any recommendations are helpful, thank you!
Attachments
1.png
1.png (5.46 KiB) Viewed 1568 times
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Activating a different chrome profile user

20 Sep 2021, 12:27

If you use your script to run Chrome, you can identify the HWND for each window, and then refer to the specific window as needed.

Code: Select all

#SingleInstance Force
chrome1 := chrome(100, 100), chrome2 := chrome(300, 300)
WinActivate, %chrome1%
Send http://www.autohotkey.com/`n

chrome(x, y) {
 WinMinimize, % wTitle := "ahk_exe chrome.exe"
 Run, chrome
 WinWaitActive, %wTitle%
 WinMove, x, y
 Return "ahk_id " WinActive("A")
}
bxgchoi
Posts: 16
Joined: 28 Oct 2020, 13:48

Re: Activating a different chrome profile user

20 Sep 2021, 18:55

mikeyww wrote:
20 Sep 2021, 12:27
If you use your script to run Chrome, you can identify the HWND for each window, and then refer to the specific window as needed.

Code: Select all

#SingleInstance Force
chrome1 := chrome(100, 100), chrome2 := chrome(300, 300)
WinActivate, %chrome1%
Send http://www.autohotkey.com/`n

chrome(x, y) {
 WinMinimize, % wTitle := "ahk_exe chrome.exe"
 Run, chrome
 WinWaitActive, %wTitle%
 WinMove, x, y
 Return "ahk_id " WinActive("A")
}
Thank you for the reply!

How can i go about finding the HWND for each chrome profile?
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Activating a different chrome profile user

20 Sep 2021, 19:27

WinActive("A") returns the HWND of the active window.

If you have created profiles in advance, you could do something like the following.

Code: Select all

#SingleInstance Force
default := chrome("Default", 100, 100), profile2 := chrome("Profile 2", 300, 300)
nav(default , "http://www.autohotkey.com/", False)
nav(profile2, "http://www.google.com/"    , False)
nav(default , "http://www.microsoft.com/"        ) ; New tab in default
nav(profile2, "http://www.microsoft.com/" , False) ; Same tab in profile2

chrome(profile, x, y) {             ; Run Chrome with a specific user profile
 WinMinimize, % wTitle := "ahk_exe chrome.exe"
 Run, chrome.exe --profile-directory="%profile%"
 WinWaitActive, %wTitle%
 WinMove, x, y
 Return "ahk_id " WinActive()
}

nav(profile, url, newTab := True) { ; Navigate to a URL in a browser with a Chrome profile
 If !WinExist(profile)
  Return
 SetKeyDelay, 5
 WinActivate, %profile%
 Send % newTab ? "^t" : "!d"
 Send {Text}%url%`n
}
bxgchoi
Posts: 16
Joined: 28 Oct 2020, 13:48

Re: Activating a different chrome profile user

22 Sep 2021, 18:26

mikeyww wrote:
20 Sep 2021, 19:27
WinActive("A") returns the HWND of the active window.

If you have created profiles in advance, you could do something like the following.

Code: Select all

#SingleInstance Force
default := chrome("Default", 100, 100), profile2 := chrome("Profile 2", 300, 300)
nav(default , "http://www.autohotkey.com/", False)
nav(profile2, "http://www.google.com/"    , False)
nav(default , "http://www.microsoft.com/"        ) ; New tab in default
nav(profile2, "http://www.microsoft.com/" , False) ; Same tab in profile2

chrome(profile, x, y) {             ; Run Chrome with a specific user profile
 WinMinimize, % wTitle := "ahk_exe chrome.exe"
 Run, chrome.exe --profile-directory="%profile%"
 WinWaitActive, %wTitle%
 WinMove, x, y
 Return "ahk_id " WinActive()
}

nav(profile, url, newTab := True) { ; Navigate to a URL in a browser with a Chrome profile
 If !WinExist(profile)
  Return
 SetKeyDelay, 5
 WinActivate, %profile%
 Send % newTab ? "^t" : "!d"
 Send {Text}%url%`n
}
oh jeez, maybe i have some more research to do. i dont know what any of these means besides my original script Lol
User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Activating a different chrome profile user

22 Sep 2021, 19:36

Line 2 opens one window for each profile, and then moves the window. The nav lines then navigate to a URL, specifying which profile, and whether to use the current tab or a new tab.

"How can i make sure that the script activates the correct window or profile?" The nav function enables you to specify the profile.

"Is there a different name for the other profiles?" The names of the profiles are whatever you named them when you created them.
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Activating a different chrome profile user

22 Sep 2021, 20:57

You can actually get the profile name with acc then filter and activate specific profiles using that

Code: Select all

^1::ActivateChromeByProfile("Person 1")

ActivateChromeByProfile(target_profile) {
    static acc_lib := Acc_Init()
    WinGet, ChromeWindow, List, ahk_exe chrome.exe
    Loop % ChromeWindow
    {
        this_hwnd := ChromeWindow%A_Index%
        if (Chrome_GetProfile(this_hwnd) == target_profile) {
            WinActivate % "ahk_id " this_hwnd
            break
        }
    }
}

Chrome_GetProfile(hwnd:=""){
	If !hwnd
    	hwnd := WinActive("A")
    try {
        title := Acc_ObjectFromWindow(hwnd).accName
        RegExMatch(title, "^.+Google Chrome . .*?([^(]+[^)]).?$", match)
        return match1
    } catch {
        return ""
    }
}

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], roeleboele and 364 guests