3

I am new to AHK and am trying to remap some of my keys to open and close certain applications on my pc to make my life easier. One of them is Google Chrome, I managed to open the profile I want of chrome but I am having trouble closing that specific profile. Below is what I have.

PgDn::
    Process, Exist, chrome.exe
If ErrorLevel <> 0
    Process, Close, chrome.exe
Else    
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1"
return

This opens up the chrome to a profile I want but when I press the button again it closes any and every chrome window open. I understand that's because I close the chrome.exe process, I cannot figure out how to close that specific profile.

Ultimately what I want to do is when the Page Down key is pressed

If Chrome Profile 1 is not opened, open it.
If Chrome Profile 1 is minimized, maximize it.
and if Chrome Profile 1 is opened and maximized, close it.

If someone could help I'd really appreciate it.

Thierry Dalon
  • 779
  • 5
  • 21
Adam Johns
  • 61
  • 2
  • As I don't use Chrome profiles, I'm not quite certain what you mean by closing the opened profile. Are you wanting to switch the profile to another one in the existing Chrome session? Are you wanting to close the profile in use by killing the chrome session? Something else? – David Metcalfe Nov 14 '17 at 04:33
  • Sorry for the late response, as you know in chrome several profiles can be created. I have created 2, one for personal use and other for business; therefore each would contain their own set of settings and bookmarks. As I use the two quite regularly, it's easier for me to use AHK to make my life easier. What I want is; if I press Page Down Key to open a personal profile, pressing again will close it. What it currently does is if I have both profile (personal and business) open it closes botj on Page Down press. – Adam Johns Nov 26 '17 at 10:55

3 Answers3

0

If you want to toggle with the Page Down key (on the fysical keyboard),

You can use this autohotkey example code (AHK):

This code works on a Windows 10 System.

a - if Chrome browser is not running then run Chrome browser.

b - if Chrome browser is minimized then maximized it.

c - if Chrome browser is maximized then Close the Chrome Browser.

GroupAdd, Browser, ahk_class Chrome_WidgetWin_1 ; Chrome

; + = Shift
; ! = Alt
; ^ = Ctrl
; # = Win (Windows logo key)

a = 0 ; Empty

;a = 1 - Minimize 
;a = 2 - Minimize to Maximize 
;a = 3 - Close Chrome Browser 



PgDn::
If Winactive("ahk_group Browser")
{

if (a=1)
{
sendinput #d ; if chrome browser is active then Minimize
a = 2
}else{

if (a=3)
{
sendinput !{F4} ; !=Alt (Alt+F4 = Close Chrome Browser)
}
}
} else {

if (a=2)
{
sendinput #d 
sleep 250
sendinput !{Space} 
sleep 250
sendinput x
a = 3
} else {
If Winexist("ahk_group Browser")
{
a = 1
} else {
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 
a = 1
}
}
}
return

Tip: If you use Autohotkey together with Buttoncommander Software Click Here

You can then make on your Screen Clickable Images with autohotkey commands - for example [Page Down Picture key] + [if you remove the codeline PgDn:: you can use The same Autohotkey script] Push it with your Mouse or Touch Device and it will toggle.

stevecody
  • 658
  • 1
  • 6
  • 18
0
KillChrome() {
sProfile := "Profile 1"
sPat = chrome.exe.*--profile-directory="%sProfile%"
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process  where Name = 'chrome.exe'") {
    If RegExMatch(process.Commandline,sPat){
        sCmd= taskkill /pid process.ProcessId
        Run, %sCmd%
    }
}    
}

References: https://www.autohotkey.com/boards/viewtopic.php?t=36142 and taskkill docu https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill

Thierry Dalon
  • 779
  • 5
  • 21
0

Based on this answer: https://stackoverflow.com/a/67246903/2043349 It won't kill the process but close the windows with alt+f4 one-by-one.

Chrome_Close(sProfile :=""){
; Close all Chrome Windows matching input Profile.
; If no Profile input, close all Chrome Windows

WinGet, Win, List, ahk_exe Chrome.exe

Loop %Win% {
    WinId := Win%A_Index%
    If !(sProfile ="") {
        WinProfile := Chrome_GetProfile(WinId) 
        If Not (WinProfile = sProfile) 
            Continue
    }

    WinActivate, ahk_id %WinId%
    WinWaitActive, ahk_id %WinId% 
   
    SendInput !{f4}
} ; end loop
} ; eofun

; ------------------------------------------------------------

Chrome_GetProfile(hwnd:=""){
; sProfile := Chrome_GetProfile(hWnd)
; returns Profile string
; hWnd: Window handle e.g. output of WinActive or WinExist
; If no argument is passed, will take current active window
If !hwnd
    hwnd := WinActive("A")

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

; https://stackoverflow.com/a/62954549/2043349
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)
}
Thierry Dalon
  • 779
  • 5
  • 21