JavaScript for Automation in Yosemite

2014-09-17 00:00:00 +0100 by Alex R. Young

I seem to remember that I once wrote on DailyJS about how Apple should start using JavaScript instead of AppleScript, and the possibility of cross-platform scripting with JavaScript. It looks like this is finally happening in Yosemite, highlighted in Apple's JavaScript for Automation documentation (and HN).

What does it look like? Here's a sample for Mail:

var Mail = Application('Mail');

var message = Mail.OutgoingMessage({
  subject: 'Hello world',
  visible: true


It goes further, though, with the Objective-C bridge:

JavaScript for Automation has a built-in Objective-C bridge that offers powerful utility such as accessing the file system and building Cocoa applications.

The primary access points for the Objective-C bridge are the global properties ObjC and $.

There are mappings for Objective-C methods and data types, and you can load frameworks that are built with bridge support using ObjC.import.

This bridge layer should make it possible to build native Cocoa applications that are partly drive by JavaScript, allowing you to potentially centralise some business logic into JavaScript that can run in Apple's environment or the equivalent for .NET.

This is made possible thanks to the design of the Open Scripting Architecture, and JavaScript is the second language after AppleScript that has been added.

On the commercial project I work on our Mac team recently tested JavaScript support for our scripting API. It's definitely going to make it easier for me to automate things!