chris_arceneaux Posted August 6, 2020 Share Posted August 6, 2020 (edited) 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 Requirements 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 September 15, 2020 by chris_arceneaux updating post for latest release Link to comment
vitor Posted August 7, 2020 Share Posted August 7, 2020 12 hours ago, chris_arceneaux said: While this workflow was created using Alfred 4, it should also work with Alfred 2 & 3. It won’t. A Workflow created or edited in an Alfred version can’t be opened by previous versions. Link to comment
chris_arceneaux Posted August 7, 2020 Author Share Posted August 7, 2020 Thanks for the tip! I’ve edited my post accordingly. Link to comment
jopemachine Posted August 8, 2020 Share Posted August 8, 2020 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
politicus Posted August 8, 2020 Share Posted August 8, 2020 From the Alfred debug console: Quote [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
chris_arceneaux Posted August 8, 2020 Author Share Posted August 8, 2020 @jopemachine - You need to enable Applescript in Chrome. Please check requirements in my original post. @politicus - Looks like the YouTube Music tab is not found in your Chrome browser. Can you share a screenshot of the title of your YouTube Music tab? jopemachine 1 Link to comment
chris_arceneaux Posted August 20, 2020 Author Share Posted August 20, 2020 (edited) I just released a new version of the workflow with the following enhancement: Ability to control Youtube Music volume (separate from MacOS) Download Latest Workflow Here The below screenshot demonstrates setting the volume to 80%: Edited August 20, 2020 by chris_arceneaux fixing image Link to comment
chris_arceneaux Posted August 22, 2020 Author Share Posted August 22, 2020 New version has been released. Hotkey support added for increasing/decreasing volume Search method has been improved and uses already open YM tab if available Switched from Run NAppleScript to Run Script for easier debugging Download Latest Workflow Here Link to comment
chris_arceneaux Posted September 4, 2020 Author Share Posted September 4, 2020 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
chris_arceneaux Posted September 15, 2020 Author Share Posted September 15, 2020 New version has been released with the following enhancements: Enhancements added to Alfred selection menu: 'ym' now shows currently playing Artist/Song and at the end of song name is current like/disklike status: Disliked: `-----` Liked: `+++++` Download Latest Workflow Here Link to comment
niv007 Posted November 11, 2020 Share Posted November 11, 2020 v1.4.1 is working fine. Great workflow! chris_arceneaux 1 Link to comment
panickat Posted January 8, 2021 Share Posted January 8, 2021 will add support for Brave browser? Link to comment
chris_arceneaux Posted February 16, 2021 Author Share Posted February 16, 2021 On 1/8/2021 at 6:30 AM, panickat said: will add support for Brave browser? 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" to tell application "Brave Browser" Link to comment
deanishe Posted February 16, 2021 Share Posted February 16, 2021 (edited) 6 hours ago, chris_arceneaux said: This is an AppleScript limitation as it doesn't allow the application (i.e. Google Chrome) to be changed to a variable. That used to be the case, but it works now. set _app to "Finder" tell application _app to activate Edited February 16, 2021 by deanishe chris_arceneaux 1 Link to comment
vitor Posted February 16, 2021 Share Posted February 16, 2021 @chris_arceneaux This should help you support most browsers: chris_arceneaux 1 Link to comment
chris_arceneaux Posted February 16, 2021 Author Share Posted February 16, 2021 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
deanishe Posted February 16, 2021 Share Posted February 16, 2021 2 minutes ago, chris_arceneaux said: I tried this but it didn't work. Works for me. Which version of macOS are you on? Like I said, it didn’t use to work, so it’s probably new. In any case, JXA does support using variables for the app name. Link to comment
deanishe Posted February 16, 2021 Share Posted February 16, 2021 11 minutes ago, chris_arceneaux said: set theScript to "tell application \"" & _app & "\" to ...." Also, this should be: set theScript to “tell application “ & quoted form of _app & “ to ...” Link to comment
chris_arceneaux Posted February 16, 2021 Author Share Posted February 16, 2021 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 try 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
deanishe Posted February 16, 2021 Share Posted February 16, 2021 (edited) 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 February 16, 2021 by deanishe chris_arceneaux 1 Link to comment
wahyo Posted November 2, 2023 Share Posted November 2, 2023 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
abEnigma Posted August 13 Share Posted August 13 just updated to 1.4.2 in hopes that "this" is maybe fixed, but it's not. what I'm seeing that using thumbsup or thumbsdown (ym+ and ym- respectively) just toggles that like/dislike. so if ytm is playing in the background and I want to like a song (that I'm not sure if I've liked in the past already) I have to open the window and visually make sure in which case the option in the workload is redundant since I have to open the relevant window for a basic operation so might as well do anything else with the window in front of me. any ideas suggestions? Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now