Hacker News new | past | comments | ask | show | jobs | submit login
JavaScript for OS X Automation (developer.apple.com)
357 points by radley on Sept 17, 2014 | hide | past | web | favorite | 113 comments

I found this nice simple, contrasting comparison of syntax from the last discussion about this (thought it would be worth sharing).

Here is some of the new JavaScript syntax:

Here is what it looks like in AppleScript

    tell application "Mail"
        set msgs to every outgoing message whose subject is 'JavsScript'
    end tell
While the AppleScript sounds logical, the JavaScript syntax just feels so comfortable and familiar (obviously).

The problem isn't the AppleScript's wordiness, it's that knowing the exact way to phrase a command correctly is horrible (and it doesn't help AppleScript is implicitly strongly typed).

In HyperTalk you could write something like put 4 + 4, get it; or set fred to 4 + 4; or put 4 + 4 into fred. You could leave quotes off a string, or put quotes on a number, and it would usually "just work". This made HyperTalk easy for non-programmers to write, yet still easy for programmers to write well (and debug). AppleScript allows even more syntactic constructs than HyperTalk, but they all turn out to be semantically different in incompatible ways. (It's very much like they map directly onto C++'s . -> * and & directly.) So if you can set fred to 4 + 4, you probably can't put 4 + 4 into fred. (I'm pulling these examples out of my ass without looking up actual syntax because I can't be bothered.)

I went from being a competent HyperTalk programmer to "godlike" thanks to a book by Danny Goodman (The HyperTalk Bible, I think). When AppleScript came out, Apple commissioned Goodman to write the definitive book on AppleScript -- I read it and still couldn't get anywhere. AppleScript is kind of like Blender. I can eventually figure out how to do anything, but knowledge of how I did it evaporates almost instantly. It's perhaps the worst programming language I have ever actually tried to master.

Danny Goodman later wrote books on JavaScript and DHTML which, for their time, were just as great as his HyperTalk stuff, but no-one has ever been able to make AppleScript not suck (for me, anyway).

I miss HyperTalk. It was my first "love" programming language. I agree, HyperTalk was different enough from AppleScript that you could be productive in HyperTalk, but lost in AppleScript. It might be due to the fact that AppleScript is a general purpose language, but HyperTalk was essentially a limited DSL for interacting with the HyperCard GUI environment. HyperTalk's limited scope was its best and worst feature.

I'm still surprised no one's made a small open-source HyperTalk clone. A compile-to-JS-and-run-in-the-browser version would be pretty cool.

An open source HyperCard clone: https://github.com/jhawcroft/cinsimp/

An open source HyperTalk (modernized and improved in many ways) https://github.com/kreativekorp/openxion

I've used OpenXION for shell scripting once or twice, on a lark, and it was a pleasant blast from the past.

For a while, there was a wonderful third-party alternative to Applescript known as "appscript"[0]. Your example above in Python would've been:

Appscript also had bindings for Ruby and ObjC.

Unfortunately, Apple deprecated the underlying API in 10.6 that made appscript possible and never provided a suitable replacement[1].

As always though, the problem wasn't Applescript, but rather limited support for Apple Events by applications. Unless that problem is fixed, adding Javascript support to the OSA isn't going to help much.

[0] http://appscript.sourceforge.net/index.html

[1] http://appscript.sourceforge.net/status.html

Edited to add: I see the author of appscript has additional criticism linked to elsewhere from this thread - https://news.ycombinator.com/item?id=8329408

I've always found AppleScript to be a read-only language. It is obvious what it does once written, but writing it is very frustrating as you have to get the phrasing just right.

Someone should marry Perl & AppleScript: the best read/write language ever invented!

or the worst.

As Larry Wall said: "The potential for greater good goes right along with the potential for greater evil."[1]

[1]: http://www.wall.org/~larry/pm.html

PS. I always wanted to quote this :-)

True. Actually, it probably wouldn't be too hard to just write AppleScript within Perl to begin with. ;-)

Which is frustrating for programmers, but pretty neat if you share AppleScript on web boards (which seems to happen a lot) :)

The main problem with AppleScript was all of the corner cases – above doesn't look so bad but once you tried to do anything more complex you'd have a thicket of convoluted code because they didn't allow many of the basic language features we take for granted in JavaScript.

The main thing I'm curious about is the quality of the bridge with the native APIs. AppleScript had so many never-fixed areas where bugs in the underlying implementation produced nonsensical error messages (e.g. `A scripting error has occurred: Can't make «class ppth» into a «class ppth»`) which required you to get out a debugger to figure out what was really going on. Be helpful to novices right up until you pushed them over a cliff…

Right. The problem with natural-ish language processing is that it's:

   1. Inherently imprecise.
   2. Not true natural language processing, so it's not as forgiving of
      syntax errors as you'd think - making it even more difficult
      to try to say what you want to say.

#2 is right on. Making a language look like english seems silly to me. Spoken languages are ambiguous where programming languages can't be. Why try to make it look like english when really it behaves nothing like it?

Unless you had a parser that could truly read it like english.

I feel like I'm the only one that feels this way about Ruby.

Really not sure what the point of either of these would be, could you fill in more of a real word example?

That looks pretty straightforward - it retrieves a list of messages from Mail which have a particular subject. Presumably you can then perform actions on them.

The problem is the Javascript language requires you to import the Cocoa stuff


And then you get stuff like

task = $.NSTask.alloc.init task.running == task.isRunning

The AppleScript version

if application "iTunes" is running then --- Do stuff... end if

It may just be me, but in some cases the AppleScript version is easier and better.

I think you're misreading the document, I don't think you actually have to instantiate an NSTask in order to compare whether another application is running. You would just do something like this:

  Mail = Application('Mail')
  if (Mail.isRunning) { ... }

The Cocoa/Objective-C links are for building apps, not higher-level automation.

The history behind AppleScript involved being able to write it in any language, not just English. But that was a terrible idea and Apple knows that.

English (more specifically, JavaScript) is the Lingua Franca for the majority of programming, and we've all come to accept that, for better or worse, in good times and in bad, in sickness and in health.

The AppleScript example feels like COBOL - in theory it's more readable, bit it ends up being that there is just far more room for error / ambiguity.

It has not been entirely well received: http://lists.apple.com/archives/applescript-users/2014/Sep/m...

That post is extremely negative, but I don't see a single specific complaint or reply to that thread. Does he have any constructive feedback?

Given that he mentions he was in contact with the dev team, we can be pretty sure he provided feedback. Ten years ago I participated on that list and 'has' was full of opinionated feedback for everyone. He's the one who wrote appscript, mentioned elsewhere on this thread.

I'd rather have Lua to be honest... But JS is better than AppleScript in this regard. While easy to understand, it wasn't suited for large projects and debugging was kinda nasty.

The Open Scripting Architecture (OSA) used to support additional languages instead of just AppleScript, I recall that Perl was one of them in the past. Unfortunately, this changed at point. I'm hoping that Javascript support points to them re-opening this.

OSA does not support things, it's the underlying platform hook. AFAIK third parties have always been free to implement OSA bridging. In fact one bloke did exactly that[0] when JfA was first announced.

[0] http://lists.apple.com/archives/applescript-users/2014/Sep/m...

Yes, you could already do Lua (see LuaCocoa) and other languages like Python (PyObjC), Ruby (RubyCocoa/MacRuby), and even JavaScript (JSCocoa) via ScriptingBridge way back from 10.5.

Not to mention Userscript (Frontier) which was dreamy to work in in comparison to AppleScript.

I am not a big fan of JavaScript, but I think the fact it is everywhere does make this move appealing.

Ubiquity should not be an excuse for the promotion of what is, by all objective measures, a rather poor programming language.

This is especially true when it's an environment where there are numerous other languages available, and they're all so much better than JavaScript. Lua, Python, Perl, Ruby and even Tcl would all be better choices.

It's mildly excusable when it comes to web browsers, because JavaScript really is the only viable option. But that just isn't the case with a full-featured environment like OS X.

"Objectively poor"?

Who appointed you paragon of objectivity? Myself, I think JavaScript is a very good programming language.

The WWDC session on this is pretty epic. https://developer.apple.com/videos/wwdc/2014/#306

Also, I'll just leave this here: https://www.quora.com/What-is-the-most-valuable-programming-...

That WWDC session changed my world. You really can do amazing things with the JavaScript <-> ObjC bridge.

Checkout: https://github.com/node-app/Nodelike, and my own side project https://github.com/cucumber-instruments/cucumber-instruments

Previous discussion from a few months ago: https://news.ycombinator.com/item?id=7862973

The general consensus at the time was that this was an experimental replacement/supplement to AppleScript.

I'd like to be able to use something as a universal scripting language. I'd much prefer javascript to something like bash or batch files.

Recall the “Joel test”.


In particular, item two:

  2. Can you make a build in one step?
I take that one pretty seriously. If I have piece of software I'd like people to be able to use, I'd like to be able to tell them to do the following:

  * git clone <url>
  * ./local_clone/bootstrap_script.js
Ok, that's two steps but the point stands. Hopefully they already have git and a javascript runtime included with a fresh install of their operating system (assume they just brought the computer home from the store), and the script included in the project takes care of everything else from there.

What I definitely don't want to have to say is “Set up this whole massive toolchain for language X [and Y [and Z]] before you can run the project.”

Can this be done today on the latest releases of, say, Windows, OSX, and the most popular two or three Linux distributions? If not, are we close?

Naturally there's already a npm module for interacting with it: https://github.com/brandonhorst/node-osa (note the git repo is at 0.1.0, a couple months newer than its published npmjs counterpart at 0.0.2).

Anyway, the author gives a nice, simple overview of limitations of this approach, as well as simple reasonable use cases for it.

I saw this a couple of months ago when Yosemite was first announced. I'm pretty damn excited now.

Now if these APIs were usable via node-webkit...

This seems interesting because it feels like I can gain more control over the desktop which previously I felt limited to by Bash or Python. Being a web dev I never wanted to get into Obj. C or Swift so this seems like it's opening some doors to a broader audience. It's funny they are showcasing Mail - does anyone actually use the desktop mail client? I thought that's been dead since 2003?

Can you leverage this API from within Node.js running on OSX ? Then we can start to see some cool, realtime desktop stuff.

EG: How I think this could be used for a developer: "Applet" or script that opens up a dev project you're working on. - Open Sublime text (or whatever IDE) - Pull latest down from Git - Fire up your respective local server if needed - Run tests - Release unicorns if everything goes Green through Desktop notifications of some type

It's funny they are showcasing Mail - does anyone actually use the desktop mail client? I thought that's been dead since 2003?

I'm not sure if you're referring to OS X Mail in particular or desktop mail clients in general, but I personally still use the latter (Thunderbird) very frequently. Binding all of your webmail addresses into a single local application is quite convenient.

I have been using mailmate as a successor to thunderbird because thunderbird's search was so slow amongst many other show stopping bugs for me. It's worth checking it out if you search your mail a lot or have power user mail needs... The smart folders are a neat feature that are very powerful and fast.

I have used AppleScript to open the current finder location in a new iTerm tab and to open all selected files in vim or the folder if nothing's selected... AppleScript is terrible but the end product is useful.

Another one was to merge a folder of PDFs into one document and another to convert a bunch of images into one PDF. Useful stuff but a bit awkward to create it initially.

>It's funny they are showcasing Mail - does anyone actually use the desktop mail client? I thought that's been dead since 2003?

Were did you get that idea? Extrapolating your personal usage habits?

Mail.app is one of the most frequently used OS X programs. From 30+ Mac users I know, most of them use Mail.

Not everybody has switched to webmail (or wants to).

I personally use it with a Gmail account.

>It's funny they are showcasing Mail - does anyone actually use the desktop mail client? I thought that's been dead since 2003?

I switched away from OS X in 2011, and I still miss Mail.app.

OSA was always AppleScript, and it's not really that bad of a language.

I don't agree - it's a hideous, imprecise, undocumented language. Truly awful to work with.

AppleScript is very thoroughly documented?

And I don't think hideous is a complaint, hideous is a personal opinion.

Imprecise, you'll have to expand on.

While certain things, depending on you want to do turn into a wild goose chase requiring multiple hundred lines of code to get something simple. That will not change with JavaScript, in some instances it looks like it may require even more lines with JavaScript. And it is a natural language, language. I don't think it is as terrible as you make it out too be.

AppleScript is very thoroughly documented?

As a recent Mac OS user, there were a bunch of things I wanted to automate with AS. I looked and looked for documentation and basically never found a coherent set of docs anywhere. So, I'd (still) love to see the thorough documentation.

Well, AppleScript is well documented. See Apple's Developer Site,


But, that's just AppleScript. The problem is that the scripting dictionaries for applications are inconsistent. But, that's the fault of the developers and you'll run into the same problem with Javascript.

Oh, for a good overall site, I'd recommend Mac OS X Automation,


which has pointers to a lot of resources for AppleScript, Automator, and Services.

The best documentation is Matt Neuburg's _AppleScript: The Definitive Guide_ [1] although it may be a bit dated now. Matt is one of the most talented tech writers out there.

[1]: http://shop.oreilly.com/product/9780596102111.do

When OSX first came on the scene there was a javascript OSA. A company called "Late Night Software" or some such had a drop in that allowed you to use js.

For those curious about the history of AppleScript, how it was developed, etc:


I wrote up a script a couple of months ago using JS for Automation that will automatically rotate a Keynote file from portrait to landscape (for use in prototyping on Keynote for iOS that only supports landscape). It was my first experience writing any automation script, so there was a lot of trail and error and google searches. I personally would much rather work in JS than Applescript. Here is the script in case you want to take a look at some more JS for Automation:


This is an ok idea, but go look at the API for iPhoto, Contacts or iTunes and you'll realize it is irrelevant what scripting language is used. Really sucks that Apple doesn't open up their apps to scripting better.

Apple has its issues with scripting in its apps but other companies like Microsoft and Adobe have robust scripting support in their applications. Developers like me have created significant workflows using scripting that save my company money, time, and errors. Apps like iPhoto and iTunes are really small potatoes by comparison. Having Javascript with Cocoa for scripting is truly powerful stuff.

Adobe's scripting APIs are usually 2 versions behind in functuality and very buggy. Anyone with significant workflows that script Adobe apps got there through lots of pain and suffering.

The grass isn't always greener.

Yes, I know because I script for them all the time, but at least Adobe's API's try to be more robust than Apple's. My point was really more towards there being more to scripting than just Apple's own apps.

my experience as well; i doubt any app has such extensive scripting support as Microsoft Excel; OmniGroup's apps omnioutliner and omnigrafle also had excellent scripting support--demonstrable just by opening up an app's 'dictionary' using applescript editor (and comparing those dictionaries w/ the dictionary iphoto, itunes, etc.)

Look no further than BBEdit, which has superb scripting support and is both recordable and attachable.

I recently wrote a blog post about this here: http://robbycolvin.com/javascript-automation-in-os-x-yosemit...

In Instruments since iOS 7 this has been somewhat possible but the JavaScript context was fairly locked down. I wanted to drive my UI automation tests with Cucumber (Ruby) so I wrote a little driver that patches Instruments to get access to the JavaScript context and expose it to a rudimentary server which listens for commands from your Cucumber tests.

You can check it out here https://github.com/cucumber-instruments/cucumber-instruments

Have you used Appium? It does pretty much the same thing. I have an alternative Ruby library for it too:


You can use it with Cucumber or Rspec or whatever.

Yes, when I wrote this some time late last year I vaguely recall that Appium wasn't quite able to fit my need. It looks much nicer now.

I want something even less complex for the developer to set up. I want something that shows almost no brittleness. One way to do that is to use Apple's UIAutomation directly. I also wanted to be able to use Ruby and Cucumber, so I built as simple a bridge as I could.

Selenium is popular though.

I play a stupid game (think farmville with tanks) that requires a lot of easily scripted clicking patterns.

It was pretty easy to record with automator and then have cron run that at a given interval. But that didn't work with click and drag.

Luckily there is a pretty simple way to do this with Python.

Here is a sample with clicks and dragging. http://pastebin.com/fG1d081k

You've been able to use alternative languages for a while haven't you? I remember looking into using Python at one stage and the hooks seemed fairly mature: https://developer.apple.com/library/mac/documentation/apples...

Interestingly this popped up in my RSS feed as I'm watching Sal Soghoian present what's new in 10.10 automation at http://macsysadmin.se/

No better way to learn than straight from the person who's responsible.

Now someone just needs to write the wrapper that makes the apps available as *nix processes!

In what sense? Apps show up in ps output, can be signaled with kill...

If you mean "capable of participating in plumbing," then you'll be disappointed. The app developer has the responsibility to add such functionality to consume stdin and produce to stdout.

I'm excited about the potential this will unlock. Applescript, while not too complicated, is a pain. Being able to throw together something quickly with javascript is going to make simple app scripting much more accessible.

This looks so much better than AppleScript. But, maybe, that could be just me.

Does this work with JSC REPL for trying things out? I don't have Yosemite to try it.

JSC usually is here: System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

> Does this work with JSC REPL for trying things out?

It does not: just tried it. The document lists the places it will work:

> The component can be used from Script Editor, the global Script Menu, in the Run JavaScript Automator Action, applets/droplets, the osascript command-line tool, the NSUserScriptTask API, and everywhere else other OSA components, such as AppleScript, can be used. This includes Mail Rules, Folder Actions, Address Book Plugins, Calendar Alarms, and Message Triggers.

It doesn't have a proper REPL mode, but the closest thing to what you're looking for is probably osascript.

    $ osascript -l JavaScript -i
    >> [1,2,3]
    => [1, 2, 3]

I'm not familiar with AppleScript at all, but this does seem like it could be useful.

One question I have is whether any new functionality is added or if this is just a straight replacement?

Can any AppleScript gurus chime in?

Out of the frying pan and into the fire.

Hopefully this represents a system they can easily extend to other languages in the future. There's an obvious candidate after all...

> Hopefully this represents a system they can easily extend to other languages in the future.

The system ("open scripting architecture") has been there since the 90s, before OSX. Apple used to ship support for alternative languages, and osa's always been there for third parties to interface with.

Awesome! I wonder if this could be used for end-to-end testing for iOS apps. For instance, automating turning on/off items within the Settings app.

AS is not going away yet, right? I have a ton of scripts I've written that we use at home.

Can this be used to fix the input source switch problem plaguing OS X forever?

I love this. Wish one day I can use javascript to build an ios app.


Cupertino.js already supports this. It's currently pre-alpha, but there's a JavaScript iOS app that works in the simulator.


This has the goal of eventually letting you do just that. I'm personally very excited over it!

Nimblekit[1] uses HTML + JavaScript - is that any good for you?

Edit: guh, just spotted the Sencha acquisition note, might be a dead-end.

[1] http://www.nimblekit.com/

Have you not come across Appcelerator/Titanium?


AngularJS + Cordova Framework for building hybrid apps

Ionic is amazing. I started using it last night.

PhoneGap by itself is too much of a blank canvas. This framework is opinionated and you can get something sophisticated up and running fairly quickly.

agreed, this JS support almost raises JS to first class status on osx. wish the same could be done for ios.

using cordova and similar is okaay, but not nearly as elegant as just writing the app in JS due to the tools required.

i use pythonista on my ipad. -- pretty amazing, cwoukd love the same for js/node on ios

This looks like the replacement of AppleScript

Yay. I loved that the scripting framework existed and that so many native apps implemented it, but I've hated the actual AppleScript language ever since I first learned it 20 years ago. Has we not yet learned the folly of the "natural language" programming language? Luckily I'm not old enough to have ever used COBOL.

Many of the problems of AppleScript come from inconsistent scripting support from the applications. A command in one application will work subtly different in another one. Unfortunately, this problem will continue with Javascript.

Back in the OS 9 days, there was support for additional languages instead of AppleScript. I recall that someone did a Perl implementation and I think there was a later Javascript implementation. This support was pulled at some point in the OS X days. I'm glad to see that at least Javascript has been added back.

> This support was pulled at some point in the OS X days.

The underlying tech ("open scripting architecture") has been there all along though, so third parties could have implemented OSX bridges regardless of what Apple shipped. I can only assume nobody really cared much, leading apple to remove alternative implementations and nobody to take up the slack (because nobody really cared much)

ScriptingBridge which was a layer to formally connect other languages to OSA is still there (unsure about 10.10), but neglected. It was introduced in 10.5 along with official support for PyObjC and RubyCocoa in that release. Apple also touted being able to do the same from Objective-C.

Other 3rd party language bridges such as LuaCocoa and JSCocoa provided ScriptingBridge support.

Why not support this through node?

Why should it be supported through node?

Because node + npm have much broader appeal and libraries already available, and those that Apple is offering could very easily fit into this system. For that matter, Apple could have pre-published them as npm modules to the local global store for their platform.

node/npm exists and is very vibrant. Embracing it would have been a better move imho. Much the same reason they didn't creat ATML (Apple Text Markup Language) as a system for documents that only macs can use. They probably would have been better served by embracing a JS platform that already works well on Macs with libraries/modules that would have worked well for their platform.

Grr.. I don't get why they didn't just embrace node, and release some native/platform modules for use on OSX.

Because Apple have their own Nitro JavaScript engine, so why would they use Google's V8?

Node isn't JavaScript. If you want packages/modules use CommonJS, AMD, or Harmony.

Exactly, Node isn't the same as JavaScript.. node + npm is a very vibrant cross-platform ecosystem of utilities and libraries that Apple could have fit very will into.

Yes, it's based on a different JS engine than what they use... that really isn't the point... just like Python, Ruby and Java aren't Apple platform inventions.

I am simply disappointed that Apple chose to embrace NIH instead of a broadly supported, and growing system that would have been a great fit for this purpose.

Apple just caught up with Windows Script Host (1999).

Edit: fixed typo. Thanks.

AppleScript and the Open Scripting Architecture[0] long predate OSX, and could be used by third-party scripting languages[1], that just never caught on with developers/users.

Apple had caught up with WSH before WSH even existed, the underlying technology[2] dates back to System 7 (1991)

[0] http://en.wikipedia.org/wiki/AppleScript#Open_Scripting_Arch...

[1] http://svn.python.org/projects/stackless/Python-2.4.3/dev/Ma... note how this is an OSA bridge on top of "classic" MacOS, not OSX

[2] http://en.wikipedia.org/wiki/Apple_event

Ok, that explains why I missed it.

Classic Macs were nowhere to be found in Portugal except for computer magazines and a few university labs.

AppleScript [1] was introduced in 1993, the fact that you can now do the same things AppleScript does using a different scripting language is (for virtually all practical purposes) a matter of semantics. But nice try anyway /s

[1] http://en.wikipedia.org/wiki/AppleScript

Well back in 1993 the only place I ever saw a classic Mac live was at the university. Even then it was on a tiny room just with 10 LCs. Everywhere else on campus was UNIX and PC.

The home market was divided between Atari ST, Amiga and PC.

So it explains I wasn't aware of it.

'caught', not 'catched'.

Arggghhh, drives me nuts when people vote down this type of correction. As someone that lives somewhere where the language is not my native language, it drives me crazy that people never take the time to correct my mistakes, meaning I never learn that I'm making them. Seriously people, pointing out someone's error in grammar / spelling is not a bad thing!

It's not a bad thing, but it's not something that's supposed to be on the top of a thread either. A downvote to make it stick to the bottom is proper in my opinion, I doubt anyone would downvote it to oblivion.

I would assume that WSH doesn't actually work in OS X, since 1999 and to this day.

So while it's kind of ancient, it's also completely unrelated to the topic of discussion.

It seems I remember some talk about Swift eventually replacing AppleScript but I can't find anything, does anyone know?

I predict this will be used to write malware. Many more people know JavaScript than AppleScript.

Considering that if compiled into a .app it should require Gatekeeper (AppleScript does), it's not a big worry. If it's not compiled, you can easily view the source.

Won't most popular languages be used to write malware, at some point?

Not sure if this is what OP was getting at, but it's possible that the attack vector will baloon if more 3rd party apps are pressured to provide logical scripting interfaces

Considering Apple has been locking down the system with App Store distribution and Gatekeeper (and sandboxing), the attack vector risk will probably be constrained.

Registration is open for Startup School 2019. Classes start July 22nd.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact