The Shape of Everything
A website mostly about Mac stuff, written by August "Gus" Mueller
» Acorn
» Retrobatch
» Mastodon
» Micro.blog
» Instagram
» Github
» Maybe Pizza?
» Archives
» Feed
» Micro feed
March 26, 2009
(This post is from my old, old, super old site. My views have changed over the years, hopefully my writing has improved, and there is now more than a handful of folks reading my site. Enjoy.)
JSTalk window

Let me say right off the bat- JSTalk isn't a replacement for AppleScript. I don't think anything will ever replace AppleScript and some of the amazing things I've seen done with it (especially in the publishing world). But I think of AppleScript in the same way as the original Macintosh Toolbox and Carbon after it. AppleScript was good in its day, but the time has come for something new. Something that doesn't require using a strange language, crufty apple events, or writing xml files to describe your object model. I want a language with momentum and resources behind it. I want something easy to setup and debug, where I can peek under the covers when things goes wrong.

JSTalk's goal can described like this: JSTalk is to AppleScript, what Cocoa is to Carbon.

I know, I know. It's a lofty, crazy goal. But someone has do try and it doesn't look like the solution is coming from Apple anytime soon. So I'm keeping it very simple, and very easy.

JSTalk isn't anything new. You write your scripts in JavaScript, and application communication is handled via Cocoa's distributed objects. Like PyObjc and RubyCocoa, JSTalk uses a bridge to talk to Cocoa (JSCocoa + WebKit's JavaScriptCore), so you get all the power and speed that comes with it. JSTalk comes with a little editor, a command line tool, and an automator action. If an application you want to script isn't exposed via JSTalk, you can still tell it what to do via the Cocoa Script Bridge (which is new to 10.5). Here's an example which sets your iChat status using this technique:

SBApplication.application("iChat").setStatusMessage("Happy (funball)");
Here's a JSTalk script which works on a build of Apple's Sketch sample code, with native JSTalk support added:
var sketch = JSTalk.application("Sketch"); var doc = sketch.orderedDocuments()[0]; var rectangle = doc.makeNewBox(); rectangle.setWidth(100); rectangle.setHeight(100);

Adding this ability to Sketch was about 7 lines of code, and adding the JSTalk framework to it. Seriously. No XML files, no four character event codes, and I didn't even have to sacrifice a goat to get it to work.

I've also added basic support to both VoodooPad and Acorn, via plugins. So even if an application doesn't natively support JSTalk, it might be possible to add the functionality via a loadable bundle.

JSTalk App Icon
(icon by Brad Ellis)

One of the things that has always bothered me about PyObjC and other Cocoa bridged scripting languages, is that I wasn't writing in Objective-C. The funny looking method names always bothered me:

var bezierPath = NSBezierPath.bezierPath(); bezierPath.curveToPoint_controlPoint1_controlPoint2_(pointA, pointB, pointC); bezierPath.fill();

What's with all the _'s in the method names?

Well, yes- I do know what's with all the underscores. And I know they are necessary so the bridge can convert the method names into Objective-C selectors, but it doesn't look or feel very Cocoa like to me. And since I'm using Cocoa APIs, and talking to Cocoa applications, I want to write in the same style as Cocoa uses, like so:

var bezierPath = [NSBezierPath bezierPath]; [bezierPath curveToPoint:pointA controlPoint1:pointB controlPoint2:pointC]; [bezierPath fill];

And that's my favorite part about JSTalk. You have the option to write your script in a Objective-C like style if you want to. And if you're the type of person who is scared of ['s, you don't have to use them!

That's the quick and dirty on JSTalk. The source is up on GitHub under an MIT license, you can download a pre built binary with examples to play with, and it even has a mailing list. It's very much a developer preview, and I really really want feedback on it. I hope to ship a usable 1.0 by WWDC. So if this kind of thing interests you, and you have ideas, please bring it up on the mailing list.

JSTalk source and official README: http://github.com/ccgus/jstalk/tree/master Binary: http://www.flyingmeat.com/download/private/JSTalkPreview.zip Mailing list: http://groups.google.com/group/jstalk-dev