Autohotkey 打开、重命名和最小化Google Chrome实例

Autohotkey 打开、重命名和最小化Google Chrome实例,autohotkey,Autohotkey,因此,基本上我正在尝试打开一个新的匿名浏览器,并设置选项卡的标题,以便我可以稍后激活它并最小化窗口。我认为这主要是我的变量“myIncog”设置和用法的问题 此脚本应打开一个新的chrome incognito,命名选项卡,打开一个新选项卡。然后,稍后,我想激活该选项卡,最小化整个窗口并使声音静音 不起作用的部分是查找并激活WinSetTitle I集合 ^0:: Run, C:\Program Files (x86)\Google\Chrome\Application\chrome.exe "

因此,基本上我正在尝试打开一个新的匿名浏览器,并设置选项卡的标题,以便我可以稍后激活它并最小化窗口。我认为这主要是我的变量“myIncog”设置和用法的问题

此脚本应打开一个新的chrome incognito,命名选项卡,打开一个新选项卡。然后,稍后,我想激活该选项卡,最小化整个窗口并使声音静音

不起作用的部分是查找并激活WinSetTitle I集合

^0::
Run, C:\Program Files (x86)\Google\Chrome\Application\chrome.exe " --incognito" ; works
WinActivate  ; works but probably not necessary
WinSetTitle, myIncog  ; I don't know if this works
Sleep, 1000  ; works
Send ^t  ; works - opens new tab
Return


^+0::
ifWinExist, myIncog ; no
{
MsgBox, HI! ; nope - so I know the ifWinExist does not know my WinSetTitle name 'myIncog'
WinActivate ; nope
SoundSet, +1, , mute  ; works
WinMinimize ; no
}
Return

提前谢谢

因此,您可以尝试以下方法:

^0::
    SetTitleMatchMode, 2 ; Match a window that contains WinTitle anywhere 
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --incognito

    ; Wait for the new chrome window to be created
    WinWait, - Google Chrome ahk_class Chrome_WidgetWin_1

    ; Store the hwnd for the window in a variable "chrome"
    WinGet, chrome, ID, - Google Chrome ahk_class Chrome_WidgetWin_1

    ; Activate the window using the hwnd
    WinActivate, ahk_id %chrome%

    Send {ctrl down}t{ctrl up} ; ^t should work fine, but this is less likely to let me down
    ; This could even be done with ControlSend, removing the need for WinActivate
Return

^+0::
    If WinExist("ahk_id " chrome)
    {
        MsgBox, HI!
        ; Again use the hwnd for chrome that was saved in the global variable earlier
        WinActivate ahk_id %chrome% 
        SoundSet, +1, , mute
        WinMinimize ahk_id %chrome%
    }
Return
If !WinExist("- Google Chrome ahk_class Chrome_WidgetWin_1")
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --incognito --mute-audio --new-window "about:blank",, Min
您可以根据需要修改它使用的WindowTitle,但请注意,正如您所注意到的,尝试使用WinSetTitle设置它最多只能是暂时的

导航到新网站,甚至更改选项卡,都会删除您使用WinSetTitle设置的任何内容。因此,最好使用更稳定的值,如Joe在对问题的评论中提到的hwnd

顺便说一下,您可以考虑使用来完成部分或全部需求,例如添加“-new window”和/或“-mute audio”。您可能已经知道这一点,但是您可以另外指定一个url,甚至可以指定是否希望它在启动时最小化(甚至完全隐藏)

例如,另一种方法使用了一个更加修饰的Run命令,它可能如下所示:

^0::
    SetTitleMatchMode, 2 ; Match a window that contains WinTitle anywhere 
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --incognito

    ; Wait for the new chrome window to be created
    WinWait, - Google Chrome ahk_class Chrome_WidgetWin_1

    ; Store the hwnd for the window in a variable "chrome"
    WinGet, chrome, ID, - Google Chrome ahk_class Chrome_WidgetWin_1

    ; Activate the window using the hwnd
    WinActivate, ahk_id %chrome%

    Send {ctrl down}t{ctrl up} ; ^t should work fine, but this is less likely to let me down
    ; This could even be done with ControlSend, removing the need for WinActivate
Return

^+0::
    If WinExist("ahk_id " chrome)
    {
        MsgBox, HI!
        ; Again use the hwnd for chrome that was saved in the global variable earlier
        WinActivate ahk_id %chrome% 
        SoundSet, +1, , mute
        WinMinimize ahk_id %chrome%
    }
Return
If !WinExist("- Google Chrome ahk_class Chrome_WidgetWin_1")
    Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --incognito --mute-audio --new-window "about:blank",, Min

一种解决方法是使用窗口句柄
hwnd
来唯一地选择窗口,或者甚至在启动进程时,您也可以指定
Run
来提供进程的PID