所以基本上我正在尝试打开一个新的隐身 Chrome,并设置选项卡的标题,以便我稍后可以 Winactivate 它并最小化窗口。我认为这主要是我的变量“myIncog”设置和使用的问题。
此脚本应该打开一个新的 chrome 隐身,命名选项卡,打开一个新选项卡。然后,稍后,我想激活该选项卡,最小化整个窗口并使声音静音。
不起作用的部分是找到并激活我设置的 WinSetTitle。
^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
Run Code Online (Sandbox Code Playgroud)
提前致谢!
所以,你可以尝试这样的事情:
^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
Run Code Online (Sandbox Code Playgroud)
您可以根据需要修改它使用的 WindowTitle,但请注意,尝试使用 WinSetTitle 设置它最多只会是非常短暂的,正如您所注意到的。
导航到新网站,甚至更改选项卡,都会删除您使用 WinSetTitle 设置的任何内容。因此,最好使用更稳定的值,例如 hwnd,正如 Joe 在对该问题的评论中提到的那样。
顺便说一句,您可以考虑使用其他启动标志来满足部分或全部需求,例如添加“--new-window”和/或“--mute-audio”。您可能已经知道这一点,但您还可以指定一个 url,甚至是否希望它从一开始就最小化(甚至完全隐藏)启动它。
例如,具有更完善的运行命令的替代方法可能如下所示:
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
Run Code Online (Sandbox Code Playgroud)