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.

1 Answer 1


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)


to clickClassName2(theClassName, elementnum)
    if application "Safari" is running then
            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

  • Thanks wch1zpink! Could you help me with the fast forward and rewind as well?
    – Luke
    Jan 27, 2019 at 3:29
  • @LukeYang I will give it a shot but it's getting kind of late at the moment so... It may not be ready until tomorrow
    – wch1zpink
    Jan 27, 2019 at 3:30
  • @LukeYang I updated the code for you and commented them out. Just un comment which ever one you want to use when you want to use it
    – wch1zpink
    Jan 27, 2019 at 4:06
  • @wch1zpink, thank you! but the ytp-next-button and ytp-prev-button are acutally for the next and previous video actions. I was asking for the fast-forward(like 5 seconds) and rewind, which are the effects as same as the right-arrow and left-arrow keys.
    – Luke
    Jan 27, 2019 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .