MasterNone
MasterNone

Reputation: 568

AHK Pause\Play youtube in an unfocused chrome tab

I want to pause and play a youtube video that I have playing with a shortcut key when it's not focused playing in the background. but I have trouble understanding how can I target chrome and one of its tabs.

Press shortcut key sends "spacebar" key to chrome youtube tab.

It's seems simple but I'm quite new to this any help appreciated.

Upvotes: 0

Views: 5971

Answers (3)

Albertputin Jobs
Albertputin Jobs

Reputation: 35

#Persistent
#NoEnv
#SingleInstance, Force
DetectHiddenWindows, On
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2

controlN := 0

return

#IfWinNotActive, ahk_exe chrome.exe
z & space::
    chromeSend("{Space}")
return


z & right::
    chromeSend("{Right}")
return

z & left::
    chromeSend("{Left}")
return

chromeSend(keys)
{
    ControlGet, controlN, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
    ControlFocus,,ahk_id %controlN%
    Sleep, 50
    ControlSend, Chrome_RenderWidgetHostHWND1, %keys% , Google Chrome
}



#IfWinActive, ahk_exe chrome.exe
z & space::
    chromeSend("k")
return


z & right::
    chromeSend("{Right}")
return

z & left::
    chromeSend("{Left}")
return

Using this code you can control youtube using z + space/arrows.

Upvotes: 1

Matiaan
Matiaan

Reputation: 154

This will play/pause a youtube video when not in chrome and pressing alt+shift+p, useful for listening music on youtube. It will find the youtube tab, play/pause and then return to your working tab all in the background

SetTitleMatchMode 2
controlID := 0
tabCount := 0
#IfWinNotActive, ahk_exe chrome.exe
; Play/pause
+!p::
    ControlGet, controlID, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome
    ControlFocus,,ahk_id %controlID%
    IfWinExist, YouTube
    {
        ControlSend, Chrome_RenderWidgetHostHWND1, k , Google Chrome
        return
    }
    tabCount := 0
    Loop {
        IfWinExist, YouTube
            break
        ControlSend, , ^{PgUp} , Google Chrome
        sleep 150
    tabCount := tabCount + 1
    if tabCount > 10
      return
    }
    ControlSend, , k , Google Chrome
    Loop, %tabCount% {
        ControlSend, , ^{PgDn} , Google Chrome
        sleep 150
    }
    return
#IfWinNotActive

Upvotes: 1

Schneyer
Schneyer

Reputation: 1257

A raw working script:

SetTitleMatchMode, 2

ControlGet, OutputVar, Hwnd,,Chrome_RenderWidgetHostHWND1, Google Chrome

ControlFocus,,ahk_id %outputvar%

Loop {
    IfWinExist, YouTube
        break

    ControlSend, , ^{PgUp} , Google Chrome
    sleep 300
}
ControlSend, , {Space} , Google Chrome

ctrl + PgUp is used to circle through the tabs, until a youtube tab is found, then space is send to chrome. You have to adjust the sleep timer to your system.

Not having a youtube tab or no Chrome at all will result in an infinite loop. Include safety checks!

Upvotes: 3

Related Questions