JavaScript for OS X Automation
developer.apple.com
developer.apple.com
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).ObjC.import('Cocoa')
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.
Mail = Application('Mail')
if (Mail.isRunning) { ... } 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.Unless you had a parser that could truly read it like english.
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…
app('Mail').outgoingMessages[its.subject=='JavaScript']
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
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'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 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.
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.
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 :-)
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.
Who appointed you paragon of objectivity? Myself, I think JavaScript is a very good programming language.
[0] http://lists.apple.com/archives/applescript-users/2014/Sep/m...
Also, I'll just leave this here: https://www.quora.com/What-is-the-most-valuable-programming-...
Checkout: https://github.com/node-app/Nodelike, and my own side project https://github.com/cucumber-instruments/cucumber-instruments
The general consensus at the time was that this was an experimental replacement/supplement to AppleScript.
Recall the “Joel test”.
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?
Anyway, the author gives a nice, simple overview of limitations of this approach, as well as simple reasonable use cases for it.
Now if these APIs were usable via node-webkit...
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
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.
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.
https://developer.apple.com/library/mac/documentation/AppleS...
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
which has pointers to a lot of resources for AppleScript, Automator, and Services.
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.
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.
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.
I switched away from OS X in 2011, and I still miss Mail.app.
The grass isn't always greener.
You can check it out here https://github.com/cucumber-instruments/cucumber-instruments
https://github.com/songkick/rubium-ios
You can use it with Cucumber or Rspec or whatever.
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.
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
No better way to learn than straight from the person who's responsible.
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.
JSC usually is here: System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
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]
>>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?
Hopefully this represents a system they can easily extend to other languages in the future. There's an obvious candidate after all...
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.
Edit: guh, just spotted the Sencha acquisition note, might be a dead-end.
AngularJS + Cordova Framework for building hybrid apps
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.
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 has the goal of eventually letting you do just that. I'm personally very excited over it!
Cupertino.js already supports this. It's currently pre-alpha, but there's a JavaScript iOS app that works in the simulator.
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.
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)
Other 3rd party language bridges such as LuaCocoa and JSCocoa provided ScriptingBridge support.
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.
Node isn't JavaScript. If you want packages/modules use CommonJS, AMD, or Harmony.
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.
Edit: fixed typo. Thanks.
So while it's kind of ancient, it's also completely unrelated to the topic of discussion.
The home market was divided between Atari ST, Amiga and PC.
So it explains I wasn't aware of it.
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
Classic Macs were nowhere to be found in Portugal except for computer magazines and a few university labs.