How can I use Autohotkey to focus on an existing Google Chrome tab, not a "container" window?

Solution 1:

^+i::
if WinExist("ahk_class Chrome_WindowImpl_0")
  {
  WinActivate
  ControlFocus, Chrome_AutocompleteEditView1
  }
else
  Run "C:\Users\vleeshue\AppData\Local\Google\Chrome\Application\chrome.exe"
return

Should do the trick

("Chrome_AutocompleteEditView1" is the name of the omnibar control, so you could add Send ^a to select all)

Note: To get the ahk_class for your version of Chrome, e.g., ahk_class Chrome_WindowImp1-0, use the AU3_Spy.exe inside the autohotkey directory. This will allow you to find the correct ahk class for your chrome browser if the example one doesn't work.

Update: I can't reproduce, maybe it will be better with another control... To have a list of a window controls I use this code:

#Persistent
SetTimer, WatchCursor, 100
return

WatchCursor:
  MouseGetPos, , , id, control
  WinGetTitle, title, ahk_id %id%
  WinGetClass, class, ahk_id %id%
  WinGet, ControlList, ControlList, A
  ToolTip, Under Cursor:`nahk_id: %id%`nahk_class: %class%`nTitle:%title%`nControl: %control%`n`nWindow Control List:`n%ControlList%
return

So the controls of my google chrome 4.0.249.78 beta (36714) are:

  • ViewsTextfieldEdit1
  • Chrome_RenderWidgetHostWND1
  • Chrome_AutocompleteEditView1
  • Chrome_WindowImpl_01
  • Chrome_WindowImpl_02

Solution 2:

You may want to look at using a Chrome extension instead of AutoHotkey. Extensions can get access to all the open tabs, including the URL and the ability to change tab focus. Otherwise you probably would need to use the Accessibility features in Chrome to query the active window. I believe that is how programs like RescueTime track what the active URL is. For example, using the Accessible Event Watcher (AccEvent) from the Windows 7 SDK shows the following events when changing tabs in Chrome:

Google Chrome AccEvent

Solution 3:

Workaround using Alt+Tab:

; Activates the window identified with wintitle if it's active,
; else opens a new one
OpenWindow(wintitle, runCommand)
{
    if WinExist(wintitle)
        WinActivate ; activates the window found above. Sweet.
    else
        Run %runCommand%
}

#g::
AppsKey & g::
    prevKeyDelay := A_KeyDelay
    SetKeyDelay, 100
    OpenWindow("ahk_class Chrome_WidgetWin_0", A_AppData
                . "\Local\Google\Chrome\Application\chrome.exe")
    SendEvent {Alt down}{Tab}
    SendEvent +{Tab}
    SendEvent {Alt up}
    SetKeyDelay, prevKeyDelay
return

Adjust arguments as needed. SetKeyDelay used because sending too fast does not work.

Solution 4:

if you want to find a tab of chrome you can use this

SetTitleMatchMode, 2
If WinExist("your title ahk_exe chrome.exe")

     .... do your stuff ... 

else {
     .... do your other stuff ...
}

return

since in chrome everything is a process, your tabs are also processes.