Skip to content

Instantly share code, notes, and snippets.

@khrice
Last active December 11, 2020 22:41
Show Gist options
  • Save khrice/10143186 to your computer and use it in GitHub Desktop.
Save khrice/10143186 to your computer and use it in GitHub Desktop.
Applescript to pause and play youtube videos using Alfred (In Chrome)
First go to https://www.youtube.com/html5 and request the html5 player (click the button)
Then download Alfred from the app store (https://itunes.apple.com/us/app/alfred/id405843582?mt=12)
go to alfred preferences and make sure AppleScripts is checked in the results preferences
next open a youtube video in chrome
then open alfred via the shortcut (ctrl+space or option+space) and type youtubeController (you can just start to type it and as soon as it shows up hit enter)
this is will pause or play the video for you and after you use alfred to run it a few times you should be able to open alfred and type yo and hit enter and it works.
We will be working on getting this to be bound to a keyboard shortcut and as soon as that is done we will update this with how to do that.
tell application "Google Chrome"
set found_video to false
set window_list to every window
repeat with the_window in window_list
if found_video is equal to true then
exit repeat
end if
set tab_list to every tab in the_window
repeat with the_tab in tab_list
if the title of the_tab contains "- YouTube" then
tell the_tab
execute javascript "var h = document.querySelector('[aria-label=\"Pause\"]');"
execute javascript "if(h == null) var h = document.querySelector('[aria-label=\"Play\"]');"
execute javascript "h.click()"
end tell
set found_video to true
exit repeat
end if
end repeat
end repeat
end tell
@heyMP
Copy link

heyMP commented Feb 4, 2015

You're awesome. I've been wanted this forever.

@frederickfogerty
Copy link

I've made this into a workflow, available at https://github.com/frederickfogerty/alfred-youtube. I've credited you in all the work @khrice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment