Archive for November, 2007

I'm playing with the F-Script object browser to explore the scripting capabilities of various applications on my system. Starting with the SBApplication class, I can connect to an application, visualize its scripting interface as regular methods and manipulate it interactively just like a regular object. This is using the Scripting Bridge, the Cocoa/AppleScript bridge provided by Apple in Leopard (see Fun with Leopard's Scripting Bridge – Part 1).

Quick how-to:

  1. Download and build F-Script 2.0 alpha: FScriptSources-2_0_alpha.zip
  2. Launch F-Script.app
  3. In the object browser, click on the Classes button. This lists all the classes currently linked.
  4. In the class list, click on the SBApplication class. This displays the methods of the class object.
  5. In the method list, click on the applicationWithBundleIdentifier: method and provide the required argument (i.e., a string containing the identifier of the application you want to connect to); for example, enter 'com.apple.iTunes'.
  6. You can now use the object browser to explore and experiment the scripting interface of the application.

As shown in the screenshot, the browser displays the methods of the Cocoa object automatically created by the Scripting Bridge to represent the iTunes application. To invoke a particular method, all you have to do is to click on it (the browser will asks you for arguments if needed). Once the method returns, the result is displayed in a new column in the browser and you can further explore and manipulate it. The object browser provides a lot of features to help exploring and navigating objects. You'll find a detailed description in: Exploring Cocoa with F-Script.


Read Full Post »

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:

iTunes currentTrack name

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:

iTunes nextTrack

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 »

Leopard comes with a brand new version of Interface Builder. The classic palette system is gone, replaced by a new plugin model that renders the old F-Script palette unusable.

In order to fix that, I have implemented a plugin for Interface Builder 3. It contains two elements that you can drag-and-drop from the Library window of IB (shown below) into your interface:

There is more about embedding F-Script into your own applications in this article.

Read Full Post »

In F-Script 2.0, the object browser allows navigating Objective-C 2 properties. Adding this feature was pretty easy, using the class_copyPropertyList() and property_getName() functions provided by the Objective-C runtime. The hardest part was to choose the color of the properties sections in the browser. Magenta looks fine…

No new syntax is needed to access properties using F-Script, as accessors are automatically generated by Objective-C.

Example: reading the property foo of myObject

myObject foo

Example: setting the property foo of myObject to 10

myObject setFoo:10

Read Full Post »

NSView Exploration

Visual elements in a Cocoa application are NSView objects (and have been since the early days of NEXTSTEP in the 80s). F-Script 2.0 makes it easier to select and explore them with the object browser.

  • The Select View tool, which let you select an NSView by just clicking on it on screen, now provides visual feedback. The color of the view that is under the cursor is temporarily modified to give immediate feedback as the cursor moves and to show the surface covered by the view on screen. A HUD near the cursor displays information about the view underneath.
  • SelectiViewTool.png

  • You can now exit the view selection mode without actually selecting a view, by pressing the ESCAPE key.
  • When looking at an NSView in the object browser, the view hierarchy is now displayed up front. This makes it easy to quickly explore the hierarchy, navigating down to subviews or up to the superview.


The idea of providing visual feedback as the cursor moves over views and the implementation strategy were suggested by Ken Ferry, while we were working on our F-Script Google Tech Talk just after WWDC 2007. BTW, Ken has some interesting software here, and, if you are reading this on a Mac, you are already using stuff developed by Ken, as he is one of the AppKit engineers.

Read Full Post »

Garbage Collection

New in F-Script 2.0 is a fast, concurrent, generational garbage collector. It was a lot of work to implement. Fortunately, Apple did all of it, as F-Script 2.0 uses the new Cocoa garbage collector. As with Objective-C, when you run F-Script in GC mode you no longer have to manage retain counts and to care for cyclic references. You can still use F-Script in non-GC mode and you can embed F-Script in GC or non-GC application.

Garbage collection is performed concurrently, on its own thread. Another notable aspect is that it works both for objects and for raw memory buffers. Actually, for raw memory buffers, you can either use managed or unmanaged memory. In Objective-C, managed memory (i.e. memory with automatic garbage collection) is allocated using functions such as NSAllocateCollectable(), and unmanaged memory is allocated, as usual, using functions such as malloc().

F-Script 2.0 lets you use unmanaged memory as usual (i.e., with the malloc: method) and introduces new methods, in the FSPointer class, for dealing with managed memory:

+(FSGenericPointer *)allocateCollectable:(NSUInteger)size options:(NSUInteger)options

This method allocates a collectable (i.e. managed) memory buffer with the NSAllocateCollectable() function, then creates and returns an FSGenericPointer instance pointing to this buffer, or nil if the memory cannot be allocated. If you want to pass 0 as option (which means that the memory buffer is not scanned for pointers by the garbage collector), you might prefer to invoke the shorter convenience method:

+(FSGenericPointer *)allocateCollectable:(NSUInteger)size

Features such as weak references, explicit GC control, etc. are all available from F-Script using the standard methods provided by Cocoa. For example, here is a interactive session that shows the use of NSValue to hold a weak reference that is automatically set to nil when the referenced object is collected:

> myObject := 'hello world'

> myWeakReference := NSValue valueWithNonretainedObject:myObject

> myWeakReference nonretainedObjectValue
'hello world'

> myObject := 22

> myWeakReference nonretainedObjectValue

You can control the garbage collector using the standard NSGarbageCollector Cocoa class. For example, the following code asks for a full garbage collection:

NSGarbageCollector defaultCollector collectExhaustively

The following code disables garbage collection temporarily:

NSGarbageCollector defaultCollector disable.

...do some stuff...

NSGarbageCollector defaultCollector enable. 

Read Full Post »

Gentlemen, we have rebuilt it

F-Script 2.0 is under development. An early alpha version is available for download: FScriptSources-2_0_alpha.zip.

Leopard only.

I’ll discuss some of the new features.

Read Full Post »