Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Here is what I want to do: hook up a keyboard shortcut with an action to play/pause the video that is currently open in a Google Chrome tab. Would it be possible on Mac OS X?

I was thinking about keeping Youtube always in a particular tab (e.g. the first one) and creating Automator script with a keyboard shortcut. I know it would be possible by simulating user actions (switch to particular tab, simulate click, go back to last tab) but I'm looking for more smooth way.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You could assign a shortcut to a script like this:

tell application "Google Chrome"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com" then
                execute javascript "player = document.querySelectorAll('#watch7-player embed')[0]
if (player) {
    player.getPlayerState() == 1 ? player.pauseVideo() : player.playVideo()
} else { // if youtube.com/html5 is enabled
    document.querySelectorAll('.html5-player-chrome > button:first-child')[0].click()
}"
                exit repeat
            end if
        end tell
    end repeat
end tell

For Safari, replace execute javascript with do JavaScript.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.