Send Keystrokes To A Mac App With JavaScript

Introduciton

I've been making videos with code samples. Typing and talking at the same time is hard work and tends to bring the final quality down since doing two things at once is hard. So, I wrote a script to do the typing for me.

Here's what it looks like when I send the output to Sublime Text

A text editor with text being written to it automatically including highlighting a misspelling and correcting it

And, yes. I intentionlly put a typo in there so I could show using the option and shift keys to move and select text.

The Script

The script itself uses built-in macOS JavaScript For Automation (JXA) tooling via "osascript". It's bascially Apple Script, but in JavaScript. Here's the full thing in cluding the config I used to make the GIF.

I put a few random sleep timers between each character and each snippet to make it look a little more like someone typing. You can rip all that stuff out and have it type as fast as it can too, but I like this visual better.

References