Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open, Rename, and Minimize Google Chrome Instance

Tags:

autohotkey

So basically I am trying to open a new incognito Chrome, and set the title of the tab so I can Winactivate it later and minimize the window. I think it is mostly a problem with my variable 'myIncog' setting and usage.

This script should open a new chrome incognito, name the tab, open a new tab. Then, later on, I'd like to activate that tab, minimize the entire window and mute the sound.

The part that does not work is finding and activating the WinSetTitle I set.

^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

Thanks in advance!

like image 971
Slid3r Avatar asked Nov 06 '22 13:11

Slid3r


1 Answers

So, you can try something like this:

^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

You can modify the WindowTitle it uses depending on your needs, but note that trying to set it with WinSetTitle is going to be very transient at best, as you've noticed.

Navigating to a new website, or even changing tabs, will erase whatever you set with WinSetTitle. Thus, it's better to use a more stable value, like the hwnd, as Joe mentioned in his comment on the question.

By the way, you could look into using additional launch flags to accomplish some or all of your needs, such as adding "--new-window" and/or "--mute-audio". You may already know this, but you can additionally specify a url and even whether you want it to launch it minimized (or even completely hidden) from the get-go.

For example, an alternative method with a more decked-out Run command might look like this:

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
like image 74
Shenk Avatar answered Jan 01 '23 22:01

Shenk