MacBook – How to control the Play/Stop button for a website (e.g., YouTube) in the external monitor while not having to move the mouse out of the main screen

displaymacbook pro

I want to do programming in my MacBook Pro while looking at the lectures on Udemy on my external monitor. I found it tedious that often I need to move my mouse to the external monitor and pause the video, then go back to my MacBook Pro screen and catch up with the code, then move the mouse to the external screen again and resume the video, then finally move back my mouse again to my MacBook Pro screen to resume coding. Is there a better way to do it? extensions? Maybe set up a hotkey that can pause the video but leave my mouse where it is untouched? Has anyone solved it problem in whatever the hacky way you tried? thx.

Best Answer

In the Mission Control Preferences, select the option "Displays have separate Spaces".

enter image description here

enter image description here

If you have a YouTube video playing in either Google Chrome or Safari, these code snippets will play or pause a YouTube video in either your main or secondary monitor, whether or not Google Chrome or Safari is active and visible, and whether or not the tabs are active tabs or not

Google Chrome:

to clickClassName(theClassName, elementnum)
    tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
    set youtubeTabs to item 1 of the result
    tell application "Google Chrome"
        execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
    end tell
end clickClassName    

clickClassName("ytp-play-button ytp-button", 0)

--clickClassName("ytp-prev-button ytp-button", 0)
--clickClassName("ytp-next-button ytp-button", 0)

Safari

to clickClassName2(theClassName, elementnum)
    if application "Safari" is running then
        try
            tell application "Safari"
                tell window 1 to set current tab to tab 1 whose URL contains "youtube"
                do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
            end tell
        end try
    end if
end clickClassName2

clickClassName2("ytp-play-button ytp-button", 0)

--clickClassName("ytp-prev-button ytp-button", 0)
--clickClassName("ytp-next-button ytp-button", 0)

In Automator app, create a new document and choose "Service" as the type. Add a "Run AppleScript" action to the workflow. and insert the AppleScript code

enter image description here

Save your new Automator service and name it something like... "Pause YouTube Chrome"

After this, simply go to your keyboard shortcut preferences and assign your new service a keyboard shortcut.

enter image description here

Follow that entire procedure two times. One for Google Chrome and one for Safari.

Now you should be able to play or pause YouTube videos in Chrome or Safari, available through a keyboard shortcut or the "Services" menu.

enter image description here