HOW: Close window(s) of a specific Chrome profile Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

HOW: Close window(s) of a specific Chrome profile

Post by _bonbonboy » 23 Mar 2023, 18:45

Hi here,

I wonder how, when I run the script, to make it close all windows (one or more) related to my Chrome profile 2, without impacting the other Chrome windows opened with my profile 1.

To open a specific profile, no problem, I can write something using that path "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 2".
But clearly, to close it, it's a big question mark because the name of the window(s) can be different depending on the current url, and all Chrome processes use the same name.

I found some hints here and there
https://gist.github.com/hubisan/5981dcf2a8560df9b434dd3b7d8e357b
OR
https://stackoverflow.com/questions/46906029/close-specific-chrome-profile-using-autohotkey

but I don't succeed to set up something with AHK...
Any help will be greatly appreciated, thank you beforehand :)

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: HOW: Close window(s) of a specific Chrome profile

Post by mikeyww » 23 Mar 2023, 20:12

Here is a trick to get the command lines. viewtopic.php?p=494597#p494597

You could experiment and see if it includes your Chrome profile.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: HOW: Close window(s) of a specific Chrome profile

Post by swagfag » 23 Mar 2023, 21:05

here is a rickety dogslow way of closing chrome windows that currently a certain profile open(note: the profile name is NOT the same as whatever ure passing to --profile-directory). it reads the label off of this UI element(maybe u can think of improvements how to optimize it):
image.png
image.png (1.14 KiB) Viewed 745 times

Code: Select all

#Requires AutoHotkey v2.0.2

#include UIA.ahk ; https://github.com/Descolada/UIA-v2/blob/main/Lib/UIA.ahk

q::CloseAllChromeWindowsWithProfile('Person 1')
w::CloseAllChromeWindowsWithProfile('Person 2')
e::CloseAllChromeWindowsWithProfile('Person 3')
r::CloseAllChromeWindowsWithProfile('this will error out cuz u dont have such a profile')

CloseAllChromeWindowsWithProfile := ForEachChromeWindowWithProfile.Bind(, WinClose)

ForEachChromeWindowWithProfile(profileName, action) {
    atLeastOneChromeWasFound := false

    for chromeHwnd in WinGetList('ahk_exe chrome.exe')
    {
        ChromeWindow := UIA.ElementFromWindow(chromeHwnd)

        static ThreeDotsOptionsMenuCharacteristics := {Name: 'Chrome', Type: UIA.Type.MenuItem}
        ThreeDotsOptionsMenu := ChromeWindow.FindElement(ThreeDotsOptionsMenuCharacteristics)

        ProfileCircle := ThreeDotsOptionsMenu.WalkTree('-1') ; previous sibling

        if (ProfileCircle.Name = profileName)
        {
            atLeastOneChromeWasFound := true
            action(chromeHwnd)
        }
    }

    if !atLeastOneChromeWasFound
        throw TargetError('no open chrome window found matching the specified profile', -1, profileName)  
}

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW: Close window(s) of a specific Chrome profile

Post by _bonbonboy » 24 Mar 2023, 14:57

@mikeyww Thanks, indeed this little script can be useful in several situations.
But strangely, I tried it, it works well, but having two chrome profiles open, only the default profile is mentioned:
image.png
image.png (101.69 KiB) Viewed 708 times
@swagfag Thanks for your idea. With UIA.ahk next to it and renaming the profile name by the label in your script, it works well. However, for me, that is not ideal because it requires pressing a key to close all chrome windows of a certain profile.
My goal is to add this script to a previous script. That is, once the last action of the previous script is executed, the relevant Chrome windows close, without having me to press anything.
I tried to adapt it, but it crashes all the time :/ Any ideas?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: HOW: Close window(s) of a specific Chrome profile

Post by swagfag » 24 Mar 2023, 15:48

just add it and remove the hotkeys and call the function with whatever parameter(ie profilename) u want to?

i dont know what u did, so i cant say what u did wrong nor can i infer what "crashes all the time" is supposed to mean

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW: Close window(s) of a specific Chrome profile

Post by _bonbonboy » 24 Mar 2023, 17:48

Indeed, sorry I should be more specific. I try to call the function, but for sure i do it wrong (the label of my profile is Behind):

Code: Select all

#Requires AutoHotkey v2.0.2

#include UIA.ahk ; https://github.com/Descolada/UIA-v2/blob/main/Lib/UIA.ahk

CloseAllChromeWindowsWithProfile := Behind, WinClose

ForEachChromeWindowWithProfile(profileName, action) {
    atLeastOneChromeWasFound := false

    for chromeHwnd in WinGetList('ahk_exe chrome.exe')
    {
        ChromeWindow := UIA.ElementFromWindow(chromeHwnd)

        static ThreeDotsOptionsMenuCharacteristics := {Name: 'Chrome', Type: UIA.Type.MenuItem}
        ThreeDotsOptionsMenu := ChromeWindow.FindElement(ThreeDotsOptionsMenuCharacteristics)

        ProfileCircle := ThreeDotsOptionsMenu.WalkTree('-1') ; previous sibling

        if (ProfileCircle.Name = profileName)
        {
            atLeastOneChromeWasFound := true
            action(chromeHwnd)
        }
    }

    if !atLeastOneChromeWasFound
        throw TargetError('no open chrome window found matching the specified profile', -1, profileName)  
}
The error i get:
image.png
image.png (16.82 KiB) Viewed 662 times

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: HOW: Close window(s) of a specific Chrome profile

Post by swagfag » 24 Mar 2023, 19:07

Code: Select all

CloseAllChromeWindowsWithProfile := Behind, WinClose
well, thats not the line u were supposed to edit. to keep things simpler i guess, erase it and replace it with this snippet:

Code: Select all

CloseAllChromeWindowsWithProfile(profileName) {
	ForEachChromeWindowWithProfile(profileName, WinClose)
}
then, in ur own code, at the place where u want to call the function, write:

Code: Select all

CloseAllChromeWindowsWithProfile('Behind')

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW: Close window(s) of a specific Chrome profile  Topic is solved

Post by _bonbonboy » 25 Mar 2023, 09:19

By substituting my wrong line by below as you recommended, nothing happened.

Code: Select all

CloseAllChromeWindowsWithProfile(profileName) {
	ForEachChromeWindowWithProfile(profileName, WinClose)
}
So finally, to not waste too much time, I went for a workaround (not best optimized solution I agree) keeping your initial script, changed the label at "person 1" and just added

Code: Select all

Send "q"
Adding below to my other script (still having UIA.ahk beside), it works like a charm.

Code: Select all

#include UIA.ahk ; https://github.com/Descolada/UIA-v2/blob/main/Lib/UIA.ahk

q::CloseAllChromeWindowsWithProfile('Behind')
w::CloseAllChromeWindowsWithProfile('Person 2')
e::CloseAllChromeWindowsWithProfile('Person 3')
r::CloseAllChromeWindowsWithProfile('this will error out cuz u dont have such a profile')

CloseAllChromeWindowsWithProfile := ForEachChromeWindowWithProfile.Bind(, WinClose)

ForEachChromeWindowWithProfile(profileName, action) {
    atLeastOneChromeWasFound := false

    for chromeHwnd in WinGetList('ahk_exe chrome.exe')
    {
        ChromeWindow := UIA.ElementFromWindow(chromeHwnd)

        static ThreeDotsOptionsMenuCharacteristics := {Name: 'Chrome', Type: UIA.Type.MenuItem}
        ThreeDotsOptionsMenu := ChromeWindow.FindElement(ThreeDotsOptionsMenuCharacteristics)

        ProfileCircle := ThreeDotsOptionsMenu.WalkTree('-1') ; previous sibling

        if (ProfileCircle.Name = profileName)
        {
            atLeastOneChromeWasFound := true
            action(chromeHwnd)
        }
    }

    if !atLeastOneChromeWasFound
        throw TargetError('no open chrome window found matching the specified profile', -1, profileName)  
}

Send "q"
Sleep 1000
ExitApp
Thanks again both of you for your kind help and have a great weekend :)

hisrRB57
Posts: 60
Joined: 13 Jan 2019, 11:43

Re: HOW: Close window(s) of a specific Chrome profile

Post by hisrRB57 » 20 Jul 2023, 14:21

I run into errors using chrome.ahk when I tested the example script ExportPDF.ahk. It appears that when I stopped the debugger the process was not closed and was kept active in the background and next time when I tried to connect to an existing chrome session with the same profile it picked up that process, but chrome.ahk went into a loop at:

Code: Select all

while !this.Connected
	Sleep, 50
So I looked for ways to close the processes related to a chrome profile.

I developed the following script:

Code: Select all

#Requires AutoHotkey v2.0.2

xQuote := '"'


Display_Processes()

profileName := "ChromeProfile"
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") {
    if (Process.name = "Chrome.exe") {
        PID := Process.processID
        CML := Process.commandLine
        Ipos := instr(Process.Commandline,"--user-data-dir")
        if (Ipos > 0) {
            CMLProfile := substr(Process.CommandLine,iPos+17)
            Ipos2 := instr(CMLProfile,xQuote)
            CMLProfile := Substr(CMLProfile,1,Ipos2-1)
            if (CMLProfile = profileName) {
                ProcessClose PID
            }
        }
    }
}

Display_Processes()
ExitApp

Display_Processes() {
    cnt := 0
    LVGui := Gui("+AlwaysOnTop +Resize +MinSizex500")  ; Create the ListView gui
    LVGUI.MarginX := 5
    LVGUI.MarginY := 3
    LV := LVGui.Add("ListView", "w1000 r40 -E0x200 +Multi BackgroundE4E2FC AltSubmit Count10", ["#","ID","ParentID","name","commandline"])
    for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") {
        if (Process.name = "Chrome.exe") {
            LV.Add("",++cnt,process.processID,Process.ParentProcessID,process.name,process.commandline)
            }
        }
    LV.ModifyCol()
    Listview_Rows := LV.GetCount()
    LVGui.Show("h" 500)
    winwaitclose LVGui
    return
}

First it displays all active chrome processes. In my case:
Afbeelding1.png
Afbeelding1.png (331.2 KiB) Viewed 527 times
Lines 1 and 6 of the listview shows two processes with the ChromeProfile.
What you also see is a bunch of child processes linked to those processes.
When the scripts kills the two processes of that Chromeprofile. The child processes also are removed as the second listview demonstrates:
Afbeelding2.png
Afbeelding2.png (207.92 KiB) Viewed 527 times
Feel free to comment and improve the script. My advise is to use this script for the problem described here. Do not try to close windows, but find the processes you want to kill.

Post Reply

Return to “Ask for Help (v2)”