Skip to main content

Mike Kreuzer

JavaScript for Automation - setting stuff up

17 July 2015

JXA (JavaScript for Automation) on the Mac is flaky, the dictionaries exposed by apps are shrinking and the coverage of what's available compared to what's offered in AppleScript is far from complete, but JXA's still saved me a bunch of time.

Here's a short JXA script I use to open some folders, open up terminal, cd and open Atom, my text editor of choice.

var term = Application('terminal')
term.includeStandardAdditions = true;
term.doScript('cd /Users/mike/project/ && open .');

var atom = Application('Atom');
atom.includeStandardAdditions = true;
atom.activate();
:::`

The project isn't really called project, but you get the idea, that's a lot less clicking every day, right there.

JavaScript for Automation and a major update to JavaScriptCore were both unveiled with a flourish at WWDC 2014, then both languished untouched in 2015, and JavaScript, well it's still JavaScript.

:::`JavaScript
console.assert(JavaScript === horribleLanguage, "oh yeah");
:::`

Ruby & Python are still available as part of the scripting bridge set-up, I think, but neither's even presented as an option in the stock script editor (hidden away in /Applications/Utilities/), so they're even further behind in terms of support.

I'd use Swift, I love Swift, but scripting with Swift still means building almost every single component you're going to use, from reading folder contents and files on up, all by yourself, and then rewriting all of those components every few months as the language introduces breaking changes. I've been playing around with automating git downloads & builds this week & almost everything I tried as a test had been broken on the language upgrade wheel, the dependency manager Carthage included. If you script in Swift you're still mostly on your own.

So for automating Mac apps, JXA it is, and for other automation... well, more on that soon.