I have just released a new alpha of F-Script 2.0. Included is support for the Scripting Bridge. It is available for download at FScriptSources-2_0_alpha.zip.
The Scripting Bridge, introduced in Leopard, is a Cocoa/AppleScript bridge: it allows manipulating scriptable applications as if they were Cocoa objects, automatically taking care of the communication details (e.g. creating and sending Apple Events). This is utterly cool, as it allows using F-Script to manipulate other applications through their standard scripting interface.
Now that Mac OS X provides such technology, it may be time for some refresher about AppleScript, Apple Events and so on. One very good read, providing a unique historical perspective, is AppleScript, written by William R. Cook for the HOPL III conference. There are also some interesting debates going on about the Scripting Bridge at Cocoadev and on the Applescript-implementors mailing list, including insights from Chris Nebel, of Apple, and Hamish Sanderson (aka hhas), the father of appscript. appscript is an alternative to Scripting Bridge that has been around for a while and looks interesting.
When I saw the Scripting Bridge for the first time, at WWDC 2006, it required generating glue code at design time (as if Objective-C were a mere statically typed language!) A consequence was that you had to know in advance the applications you were going to interact with at run time. Fortunately, the released version can dynamically, and automatically, creates classes at run time, lifting this limitation, and avoiding the burden of managing uninteresting generated glue code.
So how does one use Scripting Bridge in F-Script? First, to connect to an application, you use the SBApplication class provided by Leopard. For example, here is how you can connect to iTunes:
iTunes := SBApplication applicationWithBundleIdentifier:'com.apple.iTunes'
You can then manipulate iTunes from F-script. For example, let's ask iTunes for the name of the current track:
If there is no current track, an error is returned by the scripting bridge and displayed by F-Script. Otherwise, the name of the current track is displayed.
Now ask iTunes to go to the next track:
Connect to the Finder and query it for the names of the files on the desktop:
Finder := SBApplication applicationWithBundleIdentifier:'com.apple.finder'.
Finder desktop files name
Display these file names in the array inspector:
Finder desktop files name inspect
Ask the Finder for the names of the files on the desktop which have been modified today:
files := Finder desktop files.
files name at: files modificationDate >= 'today' asDate
As you see, you can make use of F-Script's array programming model as usual. Actually, this programming model (in which you manipulate whole sets of objects at once) acts synergistically with Scripting Bridge as it allows the F-Script runtime to automatically perform behind-the-scene optimizations and minimize the number of remote calls. Since such remote interactions are very costly, these optimizations have a huge impact on performance.
A few other examples:
Ask the Finder for the number of files in the Applications folder on the startup disk:
(Finder startupDisk folders objectWithName:'Applications') files count
Connect to Mail and ask for the subjects of the messages that are currently in your inbox and to which you have replied:
Mail := SBApplication applicationWithBundleIdentifier:'com.apple.mail'.
Mail inbox messages subject at: Mail inbox messages wasRepliedTo
Sweet, isn't it?
Read Full Post »