Automating Chrome with JXA (Javascript Application Scripting)


Every morning when I sit down at the computer I usually open the same browser windows: GMail, New Relic, Twitter, SoundCloud, and AWS. This usually doesn’t take too long. But I do spend a fair number of keyboard clicks to do it. One of the great things about working with OSX (macOS now) is being able to write automation scripts in JXA to simplify regularly performed tasks.

Getting Documentation

To get started lets open up the AppleScript Editor. In the top left hand corner below the red circle (record a script) button change the dropdown to JavaScript.
Now we will need to get some documentation on how to interact with Chrome. Select Window -> Library, the library has a list of programs that can be clicked to open up the JXA documentation.
Google Chrome may not be in there, you can easily add it by finding Chrome in your finder and then dragging the Google in to the Library. Be sure to change the documentation dropdown at the top to JavaScript.

Google Chrome JXA Documentation

Automating Chrome

Next let’s write a quick script that opens up a new Chrome window and a bunch of useful tabs.

Pretty straight forward! Its a very helpful script if you are opening the same websites every day.

Executing Javascript through JXA

If you are like me you spend a lot of time listening to podcasts and DJ sets on YouTube and SoundCloud. If you’re also like me you’ll usually have a lot of tabs open and the one that you are looking for is usually backgrounded in some other Space. When a call comes in or a co-worker comes by to chat you’ll need to take that time to look for the tab that is playing your media and pause it. This could take seconds! Let’s write a script to help us play and pause our media.

Play and Pause SoundCloud with JXA

Did you try and run the code in the AppleScript Editor? Did you get this error?

JavaScript Execution Error

Real simple fix. Just open up chrome and do click View -> Developer -> Allow JavaScript from Apple Events.

Pretty sweet! Now we can pair this with a Alfred Workflow with the shortcut “SCP” and we can quickly and conveniently play and pause our sets on SoundCloud.

Need to play and pause YouTube videos quickly? Easy stuff.

Play and Pause YouTube with JXA

JXA is pretty powerful and allows you to automate repetitive tasks very quickly. I use these scripts just about every day with out even thinking about it.

Is there a repetitive task that you run often that can be automated? I’d love to hear what scripts you have written and that have helped improve your workflow.

Full Stack Developer, Electronic music enthusiast, occasional tweeter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store