HOW: Close window(s) of a specific Chrome profile Topic is solved
-
- Posts: 12
- Joined: 12 Mar 2019, 17:38
HOW: Close window(s) of a specific Chrome profile
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
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
Re: HOW: Close window(s) of a specific Chrome profile
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.
You could experiment and see if it includes your Chrome profile.
Re: HOW: Close window(s) of a specific Chrome profile
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):
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)
}
-
- Posts: 12
- Joined: 12 Mar 2019, 17:38
Re: HOW: Close window(s) of a specific Chrome profile
@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: @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?
But strangely, I tried it, it works well, but having two chrome profiles open, only the default profile is mentioned: @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?
Re: HOW: Close window(s) of a specific Chrome profile
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
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
-
- Posts: 12
- Joined: 12 Mar 2019, 17:38
Re: HOW: Close window(s) of a specific Chrome profile
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):
The error i get:
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)
}
Re: HOW: Close window(s) of a specific Chrome profile
Code: Select all
CloseAllChromeWindowsWithProfile := Behind, WinClose
Code: Select all
CloseAllChromeWindowsWithProfile(profileName) {
ForEachChromeWindowWithProfile(profileName, WinClose)
}
Code: Select all
CloseAllChromeWindowsWithProfile('Behind')
-
- Posts: 12
- Joined: 12 Mar 2019, 17:38
Re: HOW: Close window(s) of a specific Chrome profile Topic is solved
By substituting my wrong line by below as you recommended, nothing happened.
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
Adding below to my other script (still having UIA.ahk beside), it works like a charm.
Thanks again both of you for your kind help and have a great weekend
Code: Select all
CloseAllChromeWindowsWithProfile(profileName) {
ForEachChromeWindowWithProfile(profileName, WinClose)
}
Code: Select all
Send "q"
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
Re: HOW: Close window(s) of a specific Chrome profile
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:
So I looked for ways to close the processes related to a chrome profile.
I developed the following script:
First it displays all active chrome processes. In my case:
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:
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.
Code: Select all
while !this.Connected
Sleep, 50
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
}
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:
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.