Control Videos Without Switching To The Browser (Draft Notes)

These are the draft notes for post [[/posts/2fwl2ezzkq9p][2fwl2ezzkq9p]]

They are not really in order and need to be edited.

Throwing them here for now in order to get the main post up without having to refine all this.

** Draft Overview

I've been learning Blender from [[][Ryan King Art's Blender Tutorails]]. They're great.

There's one part of the experience that I don't like. Constantly having to switch back and forth between Blender and Chrome so I can pause and rewind parts to watch again. That has nothing to do with Ryan King Art. It's inherent in the YouTube tutorial experience.

That kind of friction is tough for me. It makes it really hard for me to get into any type of flow which makes it harder for me to learn and makes it more likely that I'll quit.

I set out to fix that.

My goal was to create keyboard shortcuts that play/pause, rewind, and fast-forward YouTube videos playing in Chrome.

Creating shortcuts in the [[][Mac Shortcuts App]] with those snippets above pulls that off.

It's a little thing. But it makes a big difference to me. Getting and staying in a flow while learning makes it way better on every level.

Trying Alfred and got this note the first time:

“Alfred” wants access to control “Google”. Allowing control will provide access to documents and data in “Google”, and to perform actions within that app.

"Required for Alfred to be able to send automation events to this application."


  • The way I trigger these is by holding down COMMAND-OPTION-SHIFT then clicking either J, K, and L to rewind, play/pause, or fast-forward, respectively. I don't actually hold all the keys though. My [[][keyboard]] is programmable. So, I've got it setup so each action is just one button press under my left thumb.

  • The Play/Pause button is an element on the page and easy to target directly. I didn't find buttons for the forward and backwards skipping.

  • I dug through the code and figured out that it's possible to use this in the browser console to do the forward/backward skips: document.getElementsByClassName('html5-video-player')[0].seekBy(10) - Unfortunately, that doesn't work if you inject it from an OSA JXA JavaScript or AppleScript.

  • The reason the .seekBy(10) function doesn't work from the OSA script is that it's added to the <video> element by the YouTube javascript and [[][Chrome runs AppleScript commands in an isolated world]]

  • I looked at using [[][HTMLMediaElement.fastSeek()]] but it's not supported on Chrome.

  • One way that does work is to send simulated keystrokes or [[][KeyboardEvents]] for "j" and "l" to Chrome. The problem I have for that is that in order to make it work you have to switch to Chrome momentarily for it to work. While that's not a big deal it feels janky.

  • I finally found [[][HTMLMediaElement.currentTime]] which can both get and set the playhead location. That's the solution in place here.

  • It's probably possible to use [[][HTMLMediaElement]] instead of the button click approach. As long as what I've got keeps working I'm not going to worry about that.

  • It looks like there's not a direct way to tell if a video is playing in an HTMLMediaElement. [[][This video]] has this snippet designed to make the determination: !!(thePlayer.currentTime > 0 && !thePlayer.paused && !thePlayer.ended && thePlayer.readyState > 2) that didn't work for me out of the box, but I may have flubbed typing it.

  • I originally setup a script that would send key presses for the fast forward and rewind (the play/pause was able to use a straight javascript button click). The simulated keypresses meant you had to jump to the app for a split second. I didn't like that so I dug into the code and figure out the javascript only approach (TKTKTK confirm it works and you don't need to activate chrome temporarily to make it work)

  • One of the rabbit holes I went down

  • The code targets the tab that's currently visible in the front most Chrome window. You'll need to make new version if you want to address videos in other tabs.

  • This could also be done in AppleScript. I'm trying to use only OSA JavaScript so making an AppleScript version is left as an exercise for the reader.

  • I found [[][this solution]] for Ubuntu. It uses [[][playerctl]] which isn't available on the mac without compiling it yourself. (Which I'm not sure would work since it uses the [[][MPRIS D-Bus Interface]] which doesn't look is in a default Mac setup.

  • To setup Shortcuts I clicked on the Run JavaScript For Mac Automation action. It asked me for confirmation that I wanted to allow that the first time through.

  • Being able to inject JavaScript into web pages via a hotkey is super interesting to me. Figuring this out was a slog so I can't think of any ideas to use it at the moment, but I'm sure [[][I'll come up with something]]


  • [[][Run a shortcut while working on your Mac]]

  • [[][The Chromium Projects - Information for Third-party Applications on Mac]]

  • [[][HTMLMediaElement.currentTime - MDN Documentation]]

  • [[][HTMLMediaElement.fastSeek() - MDN Documentation]]

  • [[][KeyboardEvent - MDN Documentation]]

  • [[][Chromium Issue 543437: Javascript executed from Applescript doesn't have access to the page variables]]

  • [[][Video: JavaScript : How to tell if a video element is currently playing?]]

  • [[][Seek to a point in html5 video]]

  • [[][JXA problem — name of application]]

  • [[][A Beginners Guide to JXA, JavaScript Application Scripting]]

  • [[][Issue with the JXA Application.currentApplication() Object #27]]

  • [[][How do I view events fired on an element in Chrome DevTools?]]

  • [[][Creating and triggering event - MDN Documentation]]

  • [[][Applescript to run javascript on chrome]]

  • [[][List of Chromium Command Line Switches]]

  • [[][JXA-Cookbook - keystroke/using doesn't seem to work #3]]

  • [[][The Chromium Projects - Design Documents]]

  • [[][The Chromium Projects - AppleScript Support]]

  • [[][Applescript click automation in Google Chrome browser?]]

  • [[][Sending Keystrokes to Inactive Applications with Applescript]]

  • [[][Send a key code to an application without activating it first?]]

  • [[][Creating a keyboard shortcut to pause Youtube video in a Google Chrome tab]]

  • [[][Is there a way to pause a YouTube video in Google Chrome using AppleScript?]]

  • [[][Override iTunes "media" keys (play, pause, etc) for Spotify?]]

  • [[][Can I pause YouTube in Chrome from the command line?]]

  • [[][playerctl app]]

  • [[][MPRIS D-Bus Interface Specification]]

  • [[][Apple Shortcuts User Guide]]

  • [[][Apple - Intro to Shortcuts On Mac]]

  • [[][Moonlander Keyboard]]

  • [[][The Rust mpris crate]]

  • [[][MPRIS2 client in Python]]

  • [[][Chrome Apps Native Messaging]]

  • [[][Send media keys to specific application trick?]]

  • [[][Doug's AppleScripts - System Events, Key Code and Keystroke]]

  • [[][How do I automate a key press in AppleScript?]]

  • [[][MDN Documentation: dispatchEvent]]

  • [[][Creating a global configurable shortcut for MacOS apps in Swift]]