
JavaScript for OS X Automation - radley
https://developer.apple.com/library/prerelease/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/index.html
======
andyfleming
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:

    
    
      Mail.outgoingMessages.whose({subject:'JavaScript'})
    

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).

~~~
spullara
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.

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

~~~
mehmetemre
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](http://www.wall.org/~larry/pm.html)

PS. I always wanted to quote this :-)

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

------
slyfu
It has not been entirely well received:
[http://lists.apple.com/archives/applescript-
users/2014/Sep/m...](http://lists.apple.com/archives/applescript-
users/2014/Sep/msg00009.html)

~~~
STRML
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?

~~~
ellyagg
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.

------
robinhoodexe
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.

~~~
tjl
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.

~~~
masklinn
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...](http://lists.apple.com/archives/applescript-
users/2014/Sep/msg00009.html)

~~~
dottrap
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.

------
kevinSuttle
The WWDC session on this is pretty epic.
[https://developer.apple.com/videos/wwdc/2014/#306](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-...](https://www.quora.com/What-is-the-most-valuable-
programming-language-to-know-for-the-future-and-why/answer/Kevin-Suttle)

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

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

------
itafroma
Previous discussion from a few months ago:
[https://news.ycombinator.com/item?id=7862973](https://news.ycombinator.com/item?id=7862973)

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

------
charlieok
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”.

[http://www.joelonsoftware.com/articles/fog0000000043.html](http://www.joelonsoftware.com/articles/fog0000000043.html)

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?

------
chrisweekly
Naturally there's already a npm module for interacting with it:
[https://github.com/brandonhorst/node-
osa](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.

------
thomasfoster96
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...

------
cdnsteve
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

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

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

~~~
dubcanada
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.

~~~
e40
_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.

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

[https://developer.apple.com/library/mac/documentation/AppleS...](https://developer.apple.com/library/mac/documentation/AppleScript/conceptual/AppleScriptlangguide/introduction/ASLR_intro.html)

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,

[http://www.macosxautomation.com](http://www.macosxautomation.com)

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

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

[http://www.cs.utexas.edu/users/wcook/Drafts/2006/ashopl.pdf](http://www.cs.utexas.edu/users/wcook/Drafts/2006/ashopl.pdf)

------
rubymaverick
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:

[https://gist.github.com/ericallam/a5cd76651c327b116a6e](https://gist.github.com/ericallam/a5cd76651c327b116a6e)

------
milos_cohagen
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.

~~~
oatmeal_coffee
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.

~~~
teej
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.

~~~
oatmeal_coffee
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.

------
geetarista
I recently wrote a blog post about this here:
[http://robbycolvin.com/javascript-automation-in-os-x-
yosemit...](http://robbycolvin.com/javascript-automation-in-os-x-yosemite/)

------
ryanmarsh
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](https://github.com/cucumber-instruments/cucumber-instruments)

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

[https://github.com/songkick/rubium-ios](https://github.com/songkick/rubium-
ios)

You can use it with Cucumber or Rspec or whatever.

~~~
ryanmarsh
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.

------
johnpowell
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](http://pastebin.com/fG1d081k)

------
andybak
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...](https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptx/concepts/osa.html)

------
MagerValp
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/](http://macsysadmin.se/)

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

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

~~~
delinka
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.

------
andyfleming
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.

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

------
msoad
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

~~~
itafroma
> 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.

~~~
anders

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

------
bthornbury
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?

------
JonnieCache
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...

~~~
masklinn
> 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.

------
kyle_martin1
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.

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

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

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

~~~
thenovelnomad
[http://ionicframework.com/](http://ionicframework.com/)

AngularJS + Cordova Framework for building hybrid apps

~~~
achalkley
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.

------
hayksaakian
This looks like the replacement of AppleScript

~~~
__david__
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.

~~~
tjl
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.

~~~
masklinn
> 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)

~~~
dottrap
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.

------
general_failure
Why not support this through node?

~~~
c0wb0yc0d3r
Why should it be supported through node?

~~~
tracker1
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.

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

~~~
jamesrom
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.

~~~
tracker1
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.

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

Edit: fixed typo. Thanks.

~~~
masklinn
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...](http://en.wikipedia.org/wiki/AppleScript#Open_Scripting_Architecture)

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

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

~~~
pjmlp
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.

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

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

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

~~~
bthomas
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

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

