The Sleeper Hit Feature in Leopard
I almost completely forgot about this until Michael McCracken mentioned it, but one of the most significant bits about Leopard for developers (and aspiring developers) are the improvements to scripting support.First off, using AppleScript to communicate with other apps from within Objective-C is worlds better. Here's what sayeth the article:
The Scripting Bridge allows you to automatically build "glue" code to access a scriptable application with standard Objective-C method calls.
The Scripting Bridge uses native Cocoa data types, such as NSString and NSArray, requires far less code than using an NSAppleEventDescriptor, and runs more than twice as fast as a precompiled NSAppleScript.
And here's what that looks like:
NSString *currentTrackName = [[iTunes currentTrack] name];
This is very good. But that's not even the biggest news. No need to hold your breath any longer:
To open up the benefits of the Cocoa frameworks to a wider developer audience, Leopard embraces two other highly dynamic languages for use in building Cocoa applications: Ruby and Python.
The Ruby and Python bridges will be officially supported in Cocoa, including the key-value protocols. You don't have to choose just one language for your application. You can mix and match as necessary.
Good things are afoot.
The Sleeper Hit Feature in Leopard
Posted Dec 13, 2006 — 20 comments below
Posted Dec 13, 2006 — 20 comments below
Davide — Dec 13, 06 2664
I have downloaded RubyOSA and I must say I feel much more confortable scripting with Ruby than with Applescript. Accessing cocoa 'officialy' will make the ruby-scripting experience even greater.
Juan — Dec 13, 06 2665
Dale — Dec 13, 06 2666
The article's description is just too light on basic detail to determine if it's worth getting excited about the Scripting Bridge. For example, is the Scripting Bridge two way? I assume you can send data to another app, but the article doesn't say this. Can the Scripting Bridge hand data to another app, let this app transform it, and return it to the original app? How do you work out what code to write? Do you have to write an AppleScript to understand each app's Scripting Dictionary and then translate that into Cocoa?
Don't get me wrong. The Scripting Bridge seems great. And I don't expect to have Leopard's developer documentation released to the public. But it would be good if Apple gave us just enough info to know Leopard's technologies will be useful and not another limited framework.
Scott — Dec 13, 06 2667
Being able to simply toll-free bridge to a scriptable application is a huge deal. Especially if they still allow bridging between records and NSDictionaries. This'll really simplify a load of my current utility Applescript Studio stuff.
Yeah!
Scott Stevenson — Dec 13, 06 2668
I'm not sure what you mean by this, but I hardly ever use AppleScript so maybe I'm missing something obvious. I always assumed you use Script Editor's "Open Dictionary..." command to understand how to communicate with the application.
Note the key phrase is this:
Scripting Bridge allows you to automatically build "glue" code
Daniel Jalkut — Dec 13, 06 2669
So, essentially Apple is providing a proxy object ("iTunes" in the cited example) that probably implements the "valueForKey:" etc methods and creates and compiles an AppleScript to achieve the desired property set, get, or command invocation. Plus, since Apple's in charge, they can tweak for performance and everybody wins.
Pretty cool, if simple, idea.
Clint — Dec 13, 06 2670
Jim — Dec 13, 06 2672
With a game move search algorithm I did recently, I found that my Obj-C was 30x-40x faster than my Ruby version. YMMV, of course, depending on the nature of the code. For that particular computationally intensive situation, Obj-C was necessary.
For many ordinary apps, I have found Ruby to be plenty fast. Python, in my informal tests, is a bit faster than Ruby -- anywhere from 10% to 40%.
Ruby, however, is simply a joy to use. I'd use it all the time if I could. Once YARV (the on-the-fly compiler in development) is added to the Ruby distribution, Ruby should double, or more, in speed.
Scott — Dec 13, 06 2674
I suspect that what Apple have come up with is an extended framework based on the NSApplescript class. This existing class allows you to load, compile and execute Applescript from within a Cocoa application. However, it's not much fun to use if you want to do anything relatively granular (like get the name of the current iTunes track).
By using a proxy object (as you suggest) with some kind of KVC error checking based on the calling dictionary, I guess you get close to what they're s'pposedly offering.
The fun will start here tho' when working with those, uh, slightly more obtuse Applescriptable applications. There are plenty of Applescript dictionaries out there that baffle even experienced script coders. How that'll all work when you're one level removed behind a toll-free bridge could be entertaining...
mmalc — Dec 13, 06 2675
Chuck — Dec 13, 06 2677
Steven Canfield — Dec 13, 06 2679
Dale — Dec 13, 06 2680
That's good news. So we don't have to manually do this process ourselves. I was worried we'd have to read the app's dictionary, write an AppleScript to test it (every app's dictionary is different), and then transpose that to the equivalent Cocoa code. Very cool.
Rob — Dec 13, 06 2681
Chris Ryland — Dec 13, 06 2683
And, even better, the scuttlebutt (not heard from Apple, so I'm not breaking NDA ;-) is that the Python (and maybe Ruby?) bridge will use Objective-C object management (GC, etc.), so the integration will be great there as well.
Chuck — Dec 13, 06 2684
Ryan Brown — Dec 15, 06 2707
Chuck — Dec 15, 06 2708
Chris Ryland — Dec 17, 06 2737
Free papers — Dec 29, 09 7051