Piotr P
Piotr P

Reputation: 145

Move between Chrome windows using Autohotkey shortcuts

I spent last few hours trying to figure out how to use Autohotkey shortcut to switch between specific Chrome windows.

I have two monitors. Left side monitor (nr 1) is split vertically between Chrome localhost window and Vim editor. Right side monitor (nr 2) has Chrome fullscreen with my gmail, search tabs etc.

I would like to switch between the windows with ahk shortcuts e.g. Alt+1 (localhost monitor nr 1), Alt+2 (Chrome window monitor nr 2).

It's easy to do if windows have different title. I tried with titles, text, ahk_id, ahk_class, sysget (to change focus monitor), mouse clicks(covered by other windows) etc. Nothing seems to work consistently and couldn't google any sensible answer.

Any ideas?

Upvotes: 0

Views: 786

Answers (1)

Elliot DeNolf
Elliot DeNolf

Reputation: 2999

This code should work for you. One hotkey to activate the leftmost chrome window. The other to active the rightmost

CoordMode, Pixel, Screen

!1::
    ChromeList := GetWinListByClass("Chrome_WidgetWin_1")

    LeftmostPos := 9999
    LeftmostId := ""
    Loop, % ChromeList.MaxIndex()
    {
        currentId := ChromeList[A_Index][1]
        currentX := ChromeList[A_Index][2]

        if (currentX < LeftmostPos)
        {
            LeftmostPos := currentX
            LeftmostId := currentId
        }
    }

    WinActivate, % "ahk_id" LeftmostId
    Return

!2::
    ChromeList := GetWinListByClass("Chrome_WidgetWin_1")

    RightmostPos := -9999
    RightmostId := ""
    Loop, % ChromeList.MaxIndex()
    {
        currentId := ChromeList[A_Index][1]
        currentX := ChromeList[A_Index][2]

        if (currentX > RightmostPos)
        {
            RightmostPos := currentX
            RightmostId := currentId
        }
    }

    WinActivate, % "ahk_id" RightmostId
    Return


GetWinListByClass(filterClass)
{
    WinGet, all, list
    ChromeList := {}
    winCount := 1
    Loop, %all%
    {
        WinGetClass, WClass, % "ahk_id " all%A_Index%
        if (WClass = filterClass)
        {
            winId := all%A_Index%
            WinGetPos, X, Y, W, H, % "ahk_id " winId 
            ChromeList[winCount] := [winId, X]
            winCount++
        }
    }
    return ChromeList
}

Upvotes: 1

Related Questions