使用AutoHotkey关闭特定的镀 chrome 配置文件

wlsrxk51  于 2023-02-01  发布在  Go
关注(0)|答案(3)|浏览(179)

我是新来的AHK,并试图重新Map我的一些键打开和关闭某些应用程序在我的电脑上,使我的生活更容易。其中之一是谷歌浏览器,我设法打开个人资料,我想要的 chrome ,但我有麻烦关闭该特定的个人资料。以下是我有。

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

这会打开chrome到我想要的配置文件,但当我再次按下按钮时,它会关闭任何打开的chrome窗口。我知道这是因为我关闭了chrome.exe进程,我不知道如何关闭特定的配置文件。
最后我想做的是当Page Down键被按下时

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.

如果有人能帮忙我会非常感激的。

9jyewag0

9jyewag01#

基于此答案:https://stackoverflow.com/a/67246903/2043349它不会终止进程,但会使用alt+f4逐个关闭窗口。

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

omqzjyyz2#

如果要使用Page Down键(在物理键盘上)进行切换,
您可以使用以下自动热键示例代码(AHK):
此代码适用于Windows 10系统。
a -如果Chrome浏览器未运行,则运行Chrome浏览器。
B -如果Chrome浏览器最小化,然后最大化它。
c -如果Chrome浏览器已最大化,则关闭Chrome浏览器。

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

提示:如果您使用自动热键与按钮命令软件Click Here
然后,您可以使用自动热键命令在屏幕上创建可单击图像-例如[Page Down Picture key] + [if you remove codeline PgDn::你可以使用相同的Autohotkey脚本]用你的鼠标或触摸设备推它,它就会切换。

jv2fixgn

jv2fixgn3#

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

参考:https://www.autohotkey.com/boards/viewtopic.php?t=36142和任务删除文档https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill

相关问题