11 ms·
JavaScript for OS X Automation
- happyscrappy 12y agoIt seems I remember some talk about Swift eventually replacing AppleScript but I can't find anything, does anyone know?
- hayksaakian 12y agoThis looks like the replacement of AppleScript
- __david__ 12y agoYay. 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 12y agoMany 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 12y ago> 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 12y agoScriptingBridge 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.
- itafroma 12y agoPrevious 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.
- sleepychu 12y agoNow someone just needs to write the wrapper that makes the apps available as *nix processes!
- delinka 12y agoIn 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.
- robinhoodexe 12y agoI'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.
- collyw 12y agoI am not a big fan of JavaScript, but I think the fact it is everywhere does make this move appealing.
- Pacabel 12y agoUbiquity 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.
- TazeTSchnitzel 12y ago"Objectively poor"? Who appointed you paragon of objectivity? Myself, I think JavaScript is a very good programming language.
- tjl 12y agoThe 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 12y agoOSA 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/msg00009.html http://lists.apple.com/archives/applescript-users/2014/Sep/m...
- bthornbury 12y agoI'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?
- andyfleming 12y agoI'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.
- msoad 12y agoDoes 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 12y ago> 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 12y ago$ osascript -l JavaScript -i >> [1,2,3] => [1, 2, 3] >>
- andyfleming 12y agoI 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).
- rikkus 12y agoReally not sure what the point of either of these would be, could you fill in more of a real word example?
- matthewmacleod 12y agoThat looks pretty straightforward - it retrieves a list of messages from Mail which have a particular subject. Presumably you can then perform actions on them.
- dubcanada 12y agoThe problem is the Javascript language requires you to import the Cocoa stuff 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.
- linguafranca 12y agoI 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) { ... }
- oatmeal_coffee 12y ago
- rado 12y agoCan this be used to fix the input source switch problem plaguing OS X forever?
- JonnieCache 12y agoOut 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 12y ago> 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.
- thomasfoster96 12y agoI 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...
- pjmlp 12y agoApple just caught up with Windows Script Host (1999). Edit: fixed typo. Thanks.
- jacquesm 12y ago'caught', not 'catched'.
- antimagic 12y agoArggghhh, 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!
- tinco 12y agoIt'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.
- bakareika 12y agoI 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.
- w0utert 12y agoAppleScript [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 http://en.wikipedia.org/wiki/AppleScript
- pjmlp 12y ago
- mamoriamohit 12y agoThis looks so much better than AppleScript. But, maybe, that could be just me.
- c2u 12y agoI love this. Wish one day I can use javascript to build an ios app.
- petecooper 12y agoNimblekit[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/ http://www.nimblekit.com/
- thenovelnomad 12y agohttp://ionicframework.com/ http://ionicframework.com/ AngularJS + Cordova Framework for building hybrid apps
- achalkley 12y agoIonic 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.
- andyidsinga 12y agoagreed, 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
- TheAceOfHearts 12y agohttps://github.com/trueinteractions/tint2 https://github.com/trueinteractions/tint2 This has the goal of eventually letting you do just that. I'm personally very excited over it!
- jermar 12y agohttps://github.com/jerrymarino/cupertinojs https://github.com/jerrymarino/cupertinojs Cupertino.js already supports this. It's currently pre-alpha, but there's a JavaScript iOS app that works in the simulator.
- andybak 12y agoYou'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/applescript/conceptual/applescriptx/concepts/osa.html https://developer.apple.com/library/mac/documentation/apples...
- johnpowell 12y agoI 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
- rubymaverick 12y agoI 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
- slyfu 12y agoIt has not been entirely well received: http://lists.apple.com/archives/applescript-users/2014/Sep/msg00009.html http://lists.apple.com/archives/applescript-users/2014/Sep/m...
- STRML 12y agoThat 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 12y agoGiven 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.
- WoodenChair 12y agoI predict this will be used to write malware. Many more people know JavaScript than AppleScript.
- ChrisAntaki 12y agoWon't most popular languages be used to write malware, at some point?
- bthomas 12y agoNot 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 12y agoConsidering Apple has been locking down the system with App Store distribution and Gatekeeper (and sandboxing), the attack vector risk will probably be constrained.
- tjl 12y agoConsidering 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.
- milos_cohagen 12y agoThis 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 12y agoApple 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 12y agoAdobe'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 12y agoYes, 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.
- doug1001 12y agomy 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.)
- steveax 12y ago
- cdnsteve 12y agoThis 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 12y agoOSA was always AppleScript, and it's not really that bad of a language.
- matthewmacleod 12y agoI don't agree - it's a hideous, imprecise, undocumented language. Truly awful to work with.
- dubcanada 12y agoAppleScript 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 12y agoAppleScript 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.
- kyle_martin1 12y agoAwesome! 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.
- chrisweekly 12y agoNaturally 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.
- squidmccactus 12y agoHow does the async automation perform vis-a-vis an more typical systems task scripting language like Pythong. Will I see more speed in Mavericks, or should I wait for Yosemite?
- mzs 12y agoAS is not going away yet, right? I have a ton of scripts I've written that we use at home.
- js2 12y agoFor 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
- ryanmarsh 12y agoIn 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 12y agoHave 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 12y agoYes, 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.
- kevinSuttle 12y agoThe 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-language-to-know-for-the-future-and-why/answer/Kevin-Suttle https://www.quora.com/What-is-the-most-valuable-programming-...
- ryanmarsh 12y agoThat 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
- geetarista 12y agoI recently wrote a blog post about this here: http://robbycolvin.com/javascript-automation-in-os-x-yosemite/ http://robbycolvin.com/javascript-automation-in-os-x-yosemit...
- general_failure 12y agoWhy not support this through node?
- c0wb0yc0d3r 12y agoWhy should it be supported through node?
- tracker1 12y agoBecause 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 12y agoGrr.. I don't get why they didn't just embrace node, and release some native/platform modules for use on OSX.
- jamesrom 12y agoBecause 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 12y agoExactly, 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.
- MagerValp 12y agoInterestingly 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.
- charlieok 12y agoI'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?