Display music title playing in browser, and press to pause/resume

**AppleScript which can show the music title being played in a browser tab(YouTube Music in the example). **
There are 2 scripts:

  1. Main script (https://gitlab.com/raymondca/scripts/raw/master/applescripts/playing_in_browser/main.scpt): detects and displays the current playing music title
  2. Action script
    (https://gitlab.com/raymondca/scripts/raw/master/applescripts/playing_in_browser/action.scpt): pause/resume (by simulating a space keystroke) when the button is pressed

Steps to setup:

  1. Add a widget in BTT, Choose "Run Apple Script and Show Return Value", paste the main script in;
  2. In its "Predefined Action", select "Run Apple Script (blocking)", paste the action script in
    You're all set.

Screenshots (optional)
You can just drag or paste your screenshots here.

hi @raymondca,

Nice idea and good scripts. I hope I'm not overstepping, but I was inspired to fix the issue with the second script, which uses a method that brings Chrome into the foreground and flicks through the tabs in order to send the spacebar keystroke. It is very distracting, although a necessary evil to maintain the generality of your script, which I see you've written to make it easily adaptable to use with other music sites besides Youtube Music.

If, like me, however, you primarily use YouTube Music anyway, then I wrote a site-specific script that will Play/Pause it without the need to shift application focus or change active tabs. It utilises in-browser JavaScript, so View > Developer > Allow JavaScript From Apple Events will need to be ticked to allow AppleScript to send JS commands to Chrome.

Play/Pause Action Script:

use Chrome : application "Google Chrome"

property URL : "music.youtube.com"
property _W : a reference to every window of Chrome
property _T : a reference to every tab of _W
property tab : a reference to (_T where its URL contains my URL)
property js : "document.querySelector('.play-pause-button').click()"

if Chrome is not running then return
if the number of _W = 0 or the number of my tab = 0 then return

repeat with T in the contents of my tab
	execute of T javascript js
end repeat

After I did that, I decided to do a similar re-write of your main script as well, because I noticed that, if more than one YouTube Music tab is open, the order in which they exist determines which tab's title is returned, even if it's the title of a tab that isn't currently playing any music. The fix was simple enough, which was to do a quick check to make sure the tab's title wasn't simply "YouTube Music".

Song Title Main Script:

use Chrome : application "Google Chrome"

property URL : "music.youtube.com"
property _W : a reference to every window of Chrome
property _T : a reference to every tab of _W
property tab : a reference to (_T where its URL contains my URL)
property text item delimiters : {null, " - YouTube Music", "YouTube Music"}


if Chrome is not running then return
if the number of _W = 0 or the number of my tab = 0 then return

repeat with T in my tab's contents
	set tt to T's title
	if "YouTube Music" ≠ tt then exit repeat
end repeat

return tt's text items as text

Again, like the first script, this one is fairly specific to YouTube Music, although much easier to adapt to other sites than the first. Hope you and/or other users find one or both of these useful.

Hi, @CJK
Thanks for the improvement. I think hooking up with Chrome's Apple Event is a brilliant idea. We can even do more like double tapping to play next music if BTT supports that.
Your syntax is more concise. I'm new to Apple Script, good to learn more expressive syntax. If you don't mind, I would update mine with some of your codes.
BTW, there is a minor issue with your main script: when we have chrome closed, it will bring it up again.

How odd, it didn’t with me in testing. What version of Chrome and macOS are you using ?

I’m using Chrome 69 on macOS 10.13.6.

Let me investigate and get back to you, hopefully with a fix.

I seems not related to your script. I had similar experience before. I'm now with another mac without touch bar. (macOS 10.12.6 and also Chrome 69). I ran your song title main script above in script editor, sometimes I could reproduce the problem. It seems if I get a script error when I run it the first time, and close my Chrome after that, it will be pretty likely I will have the Chrome launched if I run it again. Seem a variable scope or old value get retained issue

I've edited my two scripts with, what I hope, will fix the issue. Give it a go and report back yours findings.

Tried it. Still the same. Sometimes I quit Chrome and it gets brought back automatically by the button script. Sometimes it doesn't.
Have you tried running it in Script Editor. This seems pretty consistently reproducing an issue:

  1. Do not have BTT running. With Chrome and Script Editor closed; Start Script Editor, leave the script window blank and click run, nothing happens;
  2. Open Chrome and access YouTube Music. Paste your script in the Script Editor window, click run, you should get whatever it's supposed to get in the result;
  3. Quit Chrome. Click the Run in Script Editor again, you will see Chrome gets launched

Thanks for this. I've adjusted the scripts again, so let's see if this iteration is any more successful. If not, I might have to just use System Events to detect whether or not Chrome is running via its process rather than the application (as you did in your scripts), but I personally prefer not doing that.

I don't know why the Page Not Found, can you check it again??

Imprint | Privacy Policy