Jump to content

Youtube Music Controls for Chrome

Recommended Posts

Alfred Workflow: Youtube Music Controls for Chrome

Latest version: download here

GitHub: https://github.com/carceneaux/alfred-youtube-music-control

An Alfred workflow for controlling Youtube Music playback in an open Youtube Music tab in Chrome.

I built this workflow out of necessity as Google Play music will be retired shortly.I listen to music
most of the day at work and this helps immensely.

The workflow uses the following keywords to control playback in an open Youtube Music tab:

* 'ym': Play/Pause
* 'ym prev': Previous Song
* 'ym next': Next Song
* 'ym+': Thumbs Up Song
* 'ym-': Thumbs Down Song
* 'ym vol <volume percentage>': Set Youtube Music Volume
* 'ym search <search term>': Search Youtube Music
* 'ym shuffle': Shuffle Playlist
* 'ym repeat': Set Repeat Setting
* 'ym quit': Close Youtube Music Tab


By default, executing JavaScript through AppleScript is turned off in Chrome. To turn it on, from the menu
bar, go to View > Developer > Allow JavaScript from Apple Events.

For more information: https://support.google.com/chrome/?p=applescript




Edited by chris_arceneaux
updating post for latest release
Link to comment

It seems that doesn't work.

I've left my error log.

(Error log of `ym next`)


ERROR: Youtube Music Chrome Controls[Run NSAppleScript] {

    NSAppleScriptErrorAppName = "Google Chrome";

    NSAppleScriptErrorBriefMessage = "AppleScript\Ub97c \Ud1b5\Ud55c \Uc790\Ubc14\Uc2a4\Ud06c\Ub9bd\Ud2b8 \Uc2e4\Ud589 \Uae30\Ub2a5\Uc774 \Uaebc\Uc838 \Uc788\Uc2b5\Ub2c8\Ub2e4. \Uc774 \Uae30\Ub2a5\Uc744 \Uc0ac\Uc6a9\Ud558\Ub3c4\Ub85d \Uc124\Uc815\Ud558\Ub824\Uba74 \Uba54\Ub274 \Ubc14\Uc5d0\Uc11c \Ubcf4\Uae30 > \Uac1c\Ubc1c\Uc790 > Apple Events\Uc758 \Uc790\Ubc14\Uc2a4\Ud06c\Ub9bd\Ud2b8 \Ud5c8\Uc6a9\Uc73c\Ub85c \Uc774\Ub3d9\Ud558\Uc138\Uc694. \Uc790\Uc138\Ud55c \Ub0b4\Uc6a9\Uc740 \Ub2e4\Uc74c \Ud398\Uc774\Uc9c0\Uc5d0\Uc11c \Ud655\Uc778\Ud558\Uc138\Uc694. https://support.google.com/chrome/?p=applescript";

    NSAppleScriptErrorMessage = "Google Chrome got an error: AppleScript\Ub97c \Ud1b5\Ud55c \Uc790\Ubc14\Uc2a4\Ud06c\Ub9bd\Ud2b8 \Uc2e4\Ud589 \Uae30\Ub2a5\Uc774 \Uaebc\Uc838 \Uc788\Uc2b5\Ub2c8\Ub2e4. \Uc774 \Uae30\Ub2a5\Uc744 \Uc0ac\Uc6a9\Ud558\Ub3c4\Ub85d \Uc124\Uc815\Ud558\Ub824\Uba74 \Uba54\Ub274 \Ubc14\Uc5d0\Uc11c \Ubcf4\Uae30 > \Uac1c\Ubc1c\Uc790 > Apple Events\Uc758 \Uc790\Ubc14\Uc2a4\Ud06c\Ub9bd\Ud2b8 \Ud5c8\Uc6a9\Uc73c\Ub85c \Uc774\Ub3d9\Ud558\Uc138\Uc694. \Uc790\Uc138\Ud55c \Ub0b4\Uc6a9\Uc740 \Ub2e4\Uc74c \Ud398\Uc774\Uc9c0\Uc5d0\Uc11c \Ud655\Uc778\Ud558\Uc138\Uc694. https://support.google.com/chrome/?p=applescript";

    NSAppleScriptErrorNumber = 12;

    NSAppleScriptErrorRange = "NSRange: {382, 84}";


Link to comment

From the Alfred debug console: 




[05:19:44.699] Youtube Music Chrome Controls[Keyword] Processing complete

[05:19:44.704] Youtube Music Chrome Controls[Keyword] Passing output '' to Run NSAppleScript

[05:19:44.748] ERROR: Youtube Music Chrome Controls[Run NSAppleScript] {

    NSAppleScriptErrorBriefMessage = "The variable musicTab is not defined.";

    NSAppleScriptErrorMessage = "The variable musicTab is not defined.";

    NSAppleScriptErrorNumber = "-2753";

    NSAppleScriptErrorRange = "NSRange: {350, 8}";




Link to comment

New version has been released with the following enhancements:

  • Improved volume setting
    • Current volume is shown in Alfred results
  • Repeat control added
    • Current repeat setting is shown in Alfred results
  • Shuffle playlist control added
    • Unlike Youtube Music button, this also forces a new song to be played
  • Improved Like/Dislike setting
    • Fix ensures that current song playing is liked/disliked

Download Latest Workflow Here



Link to comment
  • 2 weeks later...
  • 1 month later...
  • 1 month later...
  • 1 month later...
On 1/8/2021 at 6:30 AM, panickat said:

will add support for Brave browser? :D


I looked into this a bit back when I was initially developing this and it's not easy to make the workflow support both Chrome and Brave. This is an AppleScript limitation as it doesn't allow the application (i.e. Google Chrome) to be changed to a variable.


My recommendation would be to install the plugin and change the scripts referencing...


tell application "Google Chrome"




tell application "Brave Browser"


Link to comment

Thanks guys! I appreciate the tips!


set _app to "Finder"
tell application _app to activate


I tried this but it didn't work. I dug in a little further and it seems a third step is necessary...


set _app to "Google Chrome"
set theScript to "tell application \"" & _app & "\" to ...."
run script theScript


With this in mind, I'll look into adding support for other browsers in the future. If anyone wants to pitch in, I'd welcome a PR🙂

Link to comment
17 minutes ago, deanishe said:

Which version of macOS are you on?


I'm running Big Sur 11.2. Here's the code I'm running that's not working:


set _app to "Google Chrome"
tell application _app to activate
    set allWins to every window
    set allTabs to {}
    repeat with currWin in allWins
        set allTabs to allTabs & every tab of currWin
    end repeat
    repeat with currTab in allTabs
            if (title of currTab) contains "Youtube Music" then set musicTab to currTab
        end try
    end repeat
    tell musicTab to execute javascript "(document.querySelector('[id=\"play-pause-button\"]')).click();"
end tell


It seems to not like multiple lines in the tell because this works:


set _app to "Google Chrome"
tell application _app to set allWins to every window


Link to comment
1 hour ago, chris_arceneaux said:

It seems to not like multiple lines in the tell because this works


Right. I think it might be a scope thing. With the tell using a variable, the compiler doesn’t understand where to find things like tab or execute javascript (i.e. it doesn't know they're defined by Chrome).


activate and window work fine because they’re defined by the OS, not individual apps (you can also tell apps that don't support AppleScript to activate, or enumerate their windows—even if you can't do anything else with them).


I guess if you want to support multiple browsers, you could either use JXA (it supports variables for app names) or save the scripts as text (.applescript files) and do a search-and-replace on them if the user changes the target browser (i.e. replace "Google Chrome" with "Brave" or whatever).

Edited by deanishe
Link to comment
  • 2 years later...

Hi! I'm using 1.4.2.


When I use the hotkeys to control the volume, I can see the volume slider moving on the Youtube Music window, but the volume to my headphones does not change. When I click on the volume slider, that's when it changes. I'm on macOS 14.0 and using a DAC.


Any ideas? Thanks!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...