How to pause YouTube video via keyboard shortcut or from menu bar? Is there a piece of software that allows one to pause (&amp; un-pause) a currently playing YouTube video (or, ideally, any online video/audio media), with a keyboard shortcut or a conveniently accessible button (e.g., a button that sits in the menu bar, located at the top-right of the screen)? The fewer necessary clicks, the better.

The key here is that I want the ability to pause the video in any application, that is, when Google Chrome is not the foremost application (e.g., TextEdit or Microsoft Word is the foremost application). 

iOS has this shortcut built-in. If one swipes from the bottom of the screen to the top, media controls appear. These controls can manipulate any and all audio that originates in a Safari tab.

My web browser is Google Chrome.

OS X El Capitan, version 10.11.6. 



I would also be open to accomplishing this with an AppleScript (that can then be assigned to a key combination in FastScripts.app). But, I cannot imagine that such a complex task is possible via AppleScript.
********** UPDATED SOLUTION **********
This update is a direct solution to the OP's original question.
This following AppleScript code will add a &quot;Play/Pause YouTube&quot; status menu item with the options to play or pause any YouTube video in either Google Chrome or Safari, whether the browsers are visible or not. Save this following AppleScript code as a &quot;stay open&quot; application in Script Editor.app.
use framework &quot;Foundation&quot;
use framework &quot;AppKit&quot;
use scripting additions

property StatusItem : missing value
property selectedMenu : &quot;&quot;
property defaults : class &quot;NSUserDefaults&quot;
property internalMenuItem : class &quot;NSMenuItem&quot;
property externalMenuItem : class &quot;NSMenuItem&quot;
property newMenu : class &quot;NSMenu&quot;

my makeStatusBar()
my makeMenus()

on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar
    set StatusItem to bar's statusItemWithLength:-1.0
    -- set up the initial NSStatusBars title
    StatusItem's setTitle:&quot;Play/Pause YouTube&quot;
    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:&quot;Custom&quot;
    newMenu's setDelegate:me (*
    Requied delegation for when the Status bar Menu is clicked  the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.*)
    StatusItem's setMenu:newMenu
end makeStatusBar

on makeMenus()
    newMenu's removeAllItems() -- remove existing menu items
    set someListInstances to {&quot;Play/Pause YouTube - Safari&quot;, &quot;Play/Pause YouTube - Chrome&quot;, &quot;Quit&quot;}
    repeat with i from 1 to number of items in someListInstances
        set this_item to item i of someListInstances
        set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:(&quot;someAction&quot; &amp; (i as text) &amp; &quot;:&quot;) keyEquivalent:&quot;&quot;)
        (newMenu's addItem:thisMenuItem)
        (thisMenuItem's setTarget:me) -- required for enabling the menu item
    end repeat
end makeMenus

on someAction1:sender
    clickClassName2(&quot;ytp-play-button ytp-button&quot;, 0)
end someAction1:

on someAction2:sender
    clickClassName(&quot;ytp-play-button ytp-button&quot;, 0)
end someAction2:

on someAction3:sender
    quit me
end someAction3:

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

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


If you want your new... Play Pause YouTube Status Menu.app to only be visible in the status menu and not in the Dock, you can right-click on the app in Finder and select the option to &quot;Show Package Contents&quot;.  In the Contents folder, open the Info.plist file in any text editor and add the following two lines.  Then save and close that file.
&lt;key&gt;LSBackgroundOnly&lt;/key&gt;
&lt;true/&gt;

If you are not comfortable editing the .plist file directly, this following AppleScript code will allow you to choose the application to hide from the Dock when it is running.
If the chosen application is already set to be hidden from the Dock, the only option you will be given is to unhide the application from being visible in the Dock while it is running… And vice versa.
This script is especially handy for a hiding &quot;stay open applications&quot; with idle handlers' application icons from appearing in the Dock while running.
property fileTypes : {&quot;com.apple.application-bundle&quot;}
property plistFileItem : &quot;  &lt;key&gt;LSBackgroundOnly&lt;/key&gt;&quot; &amp; linefeed &amp; &quot; &lt;true/&gt;&quot;

activate
set chosenApp to (choose application with prompt ¬
    &quot;Choose  The Application You Want Hidden From The Dock While It Is Running&quot; as alias)

tell application &quot;System Events&quot; to set appName to name of chosenApp
set plistFile to ((POSIX path of chosenApp) &amp; &quot;/Contents/info.plist&quot;) as string
set plistFileContents to (read plistFile)
set plistFileItemExists to plistFileItem is in plistFileContents

if plistFileItemExists then
    activate
    set theChoice to button returned of (display dialog ¬
        &quot;Would you like to un-hide &quot; &amp; quote &amp; appName &amp; quote &amp; ¬
        &quot; from the Dock while it's running?&quot; buttons {&quot;Cancel&quot;, &quot;Un-Hide&quot;} ¬
        default button 2 cancel button 1 with title &quot;Make A Choice&quot;)
else
    activate
    set theChoice to button returned of (display dialog ¬
        &quot;Would you like to hide &quot; &amp; quote &amp; appName &amp; quote &amp; ¬
        &quot; from the Dock while it's running?&quot; buttons {&quot;Cancel&quot;, &quot;Hide&quot;} ¬
        default button 2 cancel button 1 with title &quot;Make A Choice&quot;)
end if

if theChoice is &quot;Hide&quot; then
    tell application &quot;System Events&quot; to tell contents of property list file plistFile ¬
        to make new property list item at end with properties ¬
        {kind:string, name:&quot;LSBackgroundOnly&quot;, value:true}
else if theChoice is &quot;Un-Hide&quot; then
    tell application &quot;System Events&quot; to tell contents of property list file plistFile ¬
        to make new property list item at end with properties ¬
        {kind:string, name:&quot;LSBackgroundOnly&quot;, value:false}
else
    return
end if



************ ORIGINAL SOLUTION ************
This script will click on the Play/Pause button on a video playing in YouTube in Google Chrome, whether or not Google Chrome is visible.
to clickClassName(theClassName, elementnum)
    tell application &quot;Google Chrome&quot; to (tabs of window 1 whose URL contains &quot;youtube&quot;)
    set youtubeTabs to item 1 of the result
    tell application &quot;Google Chrome&quot;
        execute youtubeTabs javascript &quot;document.getElementsByClassName('&quot; &amp; theClassName &amp; &quot;')[&quot; &amp; elementnum &amp; &quot;].click();&quot;
    end tell
end clickClassName    

clickClassName(&quot;ytp-play-button ytp-button&quot;, 0)

This is the script version to work with Safari
to clickClassName2(theClassName, elementnum)
    tell application &quot;Safari&quot;
        tell window 1 to set current tab to tab 1 whose URL contains &quot;youtube&quot;
        do JavaScript &quot;document.getElementsByClassName('&quot; &amp; theClassName &amp; &quot;')[&quot; &amp; elementnum &amp; &quot;].click();&quot; in document 1
    end tell
end clickClassName2

clickClassName2(&quot;ytp-play-button ytp-button&quot;, 0)

In an effort to give the OP A complete AppleScript solution, I have taken my original answer one step further..
UPDATE
I finally figured it out.  I created an AppleScript application in Xcode.  Originally, my project only started with a one button window to control YouTube videos currently active in Chrome or Safari.  This project has grown a little bit into an application that contains several utilities.  This GIF shows the YouTube Pause button controlling YouTube in Chrome and Safari.  I linked the button actions to the AppleScript I originally wrote in script editor.

This is a snapshot of the Xcode application Working in the AppDelegate.applescript file.

Here is the code in that file I created to make the program work.
script AppDelegate

    property parent : class &quot;NSObject&quot;
    
    
    -- IBOutlets
    property theWindow : missing value
    
    to clickClassName(theClassName, elementnum) -- Handler for pausing YouTube in Chrome
        if application &quot;Google Chrome&quot; is running then
            try
                tell application &quot;Google Chrome&quot; to (tabs of window 1 whose URL contains &quot;youtube&quot;)
                set youtubeTabs to item 1 of the result
                tell application &quot;Google Chrome&quot;
                    execute youtubeTabs javascript &quot;document.getElementsByClassName('&quot; &amp; theClassName &amp; &quot;')[&quot; &amp; elementnum &amp; &quot;].click();&quot;
                end tell
            end try
        end if
    end clickClassName
    
    to clickClassName2(theClassName, elementnum) -- Handler for pausing YouTube in Safari
        if application &quot;Safari&quot; is running then
            try
                tell application &quot;Safari&quot;
                    tell window 1 to set current tab to tab 1 whose URL contains &quot;youtube&quot;
                    do JavaScript &quot;document.getElementsByClassName('&quot; &amp; theClassName &amp; &quot;')[&quot; &amp; elementnum &amp; &quot;].click();&quot; in document 1
                end tell
            end try
        end if
    end clickClassName2

    on doSomething:sender -- Calls the Chrome YouTube Handler
        clickClassName(&quot;ytp-play-button ytp-button&quot;, 0)
    end doSomething:

    on doSomething14:sender -- Calls the Safari YouTube Handler
        clickClassName2(&quot;ytp-play-button ytp-button&quot;, 0)
    end doSomething14:

    on doSomething2:sender -- Hide and or show the Menu Bar
        tell application &quot;System Preferences&quot;
            reveal pane id &quot;com.apple.preference.general&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;General&quot;
            click checkbox &quot;Automatically hide and show the menu bar&quot;
        end tell
        delay 1
        quit application &quot;System Preferences&quot;
    end doSomething2:
    
    on doSomething3:sender -- Sets Display resolution to the second lowest setting (15 inch Built In Retina Display - MBP)
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
            click radio button &quot;Scaled&quot; of radio group 1 of tab group 1
            click radio button 2 of radio group 1 of group 1 of tab group 1
        end tell
        quit application &quot;System Preferences&quot;
    end doSomething3:
    
    on doSomething4:sender -- Sets Display resolution to the second highest setting (15 inch Built In Retina Display - MBP)
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
            click radio button &quot;Scaled&quot; of radio group 1 of tab group 1
            click radio button 4 of radio group 1 of group 1 of tab group 1
        end tell
        quit application &quot;System Preferences&quot;
    end doSomething4:
    
    on doSomething5:sender -- Sets Display resolution to the highest setting (15 inch Built In Retina Display - MBP)
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
            click radio button &quot;Scaled&quot; of radio group 1 of tab group 1
            click radio button 5 of radio group 1 of group 1 of tab group 1
        end tell
        quit application &quot;System Preferences&quot;
    end doSomething5:
    
    on doSomething6:sender -- Sets Display resolution to the lowest setting (15 inch Built In Retina Display - MBP)
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
            click radio button &quot;Scaled&quot; of radio group 1 of tab group 1
            click radio button 1 of radio group 1 of group 1 of tab group 1
            delay 0.1
            click button &quot;OK&quot; of sheet 1
            quit application &quot;System Preferences&quot;
        end tell
    end doSomething6:

    on doSomething7:sender -- Displays a dialog with your current IP
        tell current application to display dialog (do shell script &quot;curl ifconfig.io&quot;) with icon 2 buttons &quot;OK&quot; default button 1 with title &quot;Your Current IP Address Is..&quot; giving up after 5
    end doSomething7:
    
    on doSomething8:sender -- Shows hidden files in Finder
        do shell script &quot;defaults write com.apple.finder AppleShowAllFiles TRUE\nkillall Finder&quot;
    end doSomething8:
    
    on doSomething9:sender -- Hides hidden files in Finder if they are showing
        do shell script &quot;defaults write com.apple.finder AppleShowAllFiles FALSE\nkillall Finder&quot;
    end doSomething9:

    on doSomething10:sender  -- Brightness Highest
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 12
        end tell
        quit application &quot;System Preferences&quot;
    end doSomething10:

    on doSomething11:sender -- Brightness Lowest
        tell application &quot;System Preferences&quot;
            reveal anchor &quot;displaysDisplayTab&quot; of pane &quot;com.apple.preference.displays&quot;
        end tell
        tell application &quot;System Events&quot; to tell process &quot;System Preferences&quot; to tell window &quot;Built-in Retina Display&quot;
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 0.1
        end tell
        quit application &quot;System Preferences&quot;
    end doSomething11:

    on doSomething12:sender -- Zoom
        tell application &quot;System Events&quot;
            key code 28 using {command down, option down}
        end tell
    end doSomething12:

    on doSomething13:sender -- Dictation On/Off
        tell application &quot;System Events&quot;
            keystroke &quot;x&quot; using {option down}
        end tell
    end doSomething13:

    on doSomething15:sender -- Enables Screensaver as Desktop background
        tell application &quot;System Events&quot;
            do shell script &quot;/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background&quot;
        end tell
    end doSomething15:

    on doSomething16:sender -- Kills Screensaver Desktop background
        try
            tell application id &quot;com.apple.ScreenSaver.Engine&quot; to quit
        end try
    end doSomething16:


    on applicationWillFinishLaunching:aNotification
        -- Insert code here to initialize your application before any files are opened

    end applicationWillFinishLaunching:
    
    on applicationShouldTerminate:sender
        -- Insert code here to do any housekeeping before your application quits

        
        return current application's NSTerminateNow
    end applicationShouldTerminate:

    on applicationShouldTerminateAfterLastWindowClosed:sender -- Quits app when clicking red x
        
        return TRUE
        
    end applicationShouldTerminateAfterLastWindowClosed:

end script

I updated the code so that the YouTube tab in Chrome doesn't need to be the visible or active tab when clicking the YouTube Pause Button created in Xcode
Here is a link to download the entire Xcode project

WARNING: The desktop screen saver function will freeze the App.  After force quit and re-open, The desktop screensaver function to exit active screen saver will work.
Afterthoughts:  I probably should have wrapped each of the AppleScript codes in &quot;try&quot; statements to avoid all sorts of error messages for those playing with this project,who don't have the same system and computer type that I do. (MacBook Pro 15&quot; OS Sierra 10.12.6)
For the zoom function To work, it must be enabled in system preferences.

For the toggling of “Dictation on/off” to function correctly, the short cut to enable dictation commands in system preferences must match the shortcut used in the script

on doSomething13:sender -- Dictation On/Off
    tell application &quot;System Events&quot;
        keystroke &quot;x&quot; using {option down}
    end tell
end doSomething13:

Currently I am working on the ability to toggle between the application running windowed or menu bar only