3

I'm trying to understand how to determine if a YouTube video/s is playing in a tab/s on a web browser (Chrome or Safari) and retrieve information like name of the video and the YouTube channel and play or pause the video. Similar to Bearded Spice. I've tried to understand the code of Bearded Spice, but I'm not familiar with Objective-C and there is no extensive documentation.

I know that tabs can be iterated thought and headings can be retrieved using AppleScript. But I'm unable to get the state of the player and some information about the video playing. I don't want to use Chrome and Safari extensions as I'm trying to achieve this using a native app for MacOS. Please point me in the right direction to achieve this.

an23lm
  • 376
  • 2
  • 15
  • I'm not quite sure what information you are trying to retrieve about the video that is playing. If you can be more specific then maybe I can adjust the code In my response a little bit. – wch1zpink Jul 14 '17 at 03:34
  • I've edited the question. I want to retrieve the name of the video and the YouTube channel. – an23lm Jul 14 '17 at 03:48
  • Yes but how do you want the information retrieved? On your clipboard? Text document? Pop-up dialog window? – wch1zpink Jul 14 '17 at 03:56
  • Just a return string from a function call should be good. As I'm going to plug this AppleScript into an Xcode project. – an23lm Jul 14 '17 at 03:58
  • Also, can I get the **player state**? Like a boolean value - `isYouTubePlaying() -> Bool`. – an23lm Jul 14 '17 at 04:04
  • I could not figure out how to get the player state. If you figure out how to do it could you please post the result? – wch1zpink Jul 14 '17 at 18:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149255/discussion-between-wch1zpink-and-an23lm). – wch1zpink Jul 14 '17 at 19:35

2 Answers2

3

This AppleScript code will Play or Paue any YouTube video which is loaded in any Google Chrome tab whether the browser is visible or not and whether the tab is visible or not. Tested on latest version of OS Sierra.

-- Google Chrome Version

to clickClassName(theClassName, elementnum)
    if application "Google Chrome" is running then
        try
            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 try
    end if
end clickClassName


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

This Returns the URLs for Chrome

tell application "Google Chrome" to URL of tabs of window 1 whose title contains "youtube"

This AppleScript code will Play or Paue any YouTube video which is loaded in the active tab of a Safari browser window

-- Safari Version

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)

This will return the URLs of the YouTube windows with the videos

tell application "Safari" to URL of tabs of window 1 whose name contains "youtube"

Similar Post With Xcode Example

wch1zpink
  • 3,026
  • 1
  • 8
  • 19
1

In addition to wch1zpink answer. You can also obtain the following:

Player State.

set playerState to execute youtubeTabs javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].getAttribute('aria-label')"

This chunk of code return the video play button's current state, i.e., "Pause" when the video is playing, "Play" when the video is paused, and "Replay" when the video is done playing.

Title of the video.

set titleCurrentVideo to execute youtubeTabs javascript "document.getElementsByTagName('title')[0].innerHTML;"

or

copy title of youtubeTabs to the end of titleList

As you iterate through the tabs the titleList is populated, where titleList is a list.

an23lm
  • 376
  • 2
  • 15