MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 http://www.macdevcenter.com/pub/a/mac/2001/12/21/browsing_cocoa.html
 See this if you're having trouble printing code examples


Browsing Cocoa with F-Script

by Philippe Mougin
12/20/2001

Editor's Note: F-Script by Philippe Mougin is a lightweight object-oriented scripting layer that provides interactive access to Cocoa frameworks and custom objects. In his first article published on the Mac DevCenter, Scripting Cocoa with F-Script, Philippe introduced O'Reilly readers to the joys of scripting their Cocoa projects. In this article, he shows you a new tool: the object browser.

Until now, in order to directly interact with your Cocoa/Objective-C objects, you had to use a programming language, be it Objective-C itself, Java via the bridge, or F-Script. Now, with the F-Script object browser, there is a new option: direct access to objects through a graphical user interface.

In this article, I'll introduce you to F-Script for Cocoa, which includes the Object Browser and can be downloaded at www.fscript.org.

The original version of this object browser was designed by Philippe Mougin and Yannick Bessy, and it was first prototyped and implemented for VisualWorks Smalltalk.

F-Script Browser Basics

Learning CocoaLearning Cocoa
By Apple Computer, Inc.
Table of Contents
Index
Sample Chapter
Full Description
Read Online -- Safari

The general principle governing usage of the object browser is very simple:

This provides unified graphical and interactive access to objects -- a very handy way to explore frameworks, test classes you're developing, or interactively use business objects.

Menu
Fig. 1. The object browser in action. Click on image for larger view.

Opening the Object Browser

If you're using the fs application, there is a menu item, "Open object browser" that does just that, displaying all the objects defined in your F-Script workspace.

In any case, you can also open the browser by executing a simple F-Script instruction:

Visualizing and Selecting Objects

Comment on this articleLet's hear the questions and comments you have for Philippe.
Post your comments

When the browser opens, it displays a textual description of an object or list of objects. To do this, the browser asks objects for their description by sending the message printString. If you are familiar with F-Script, you will know that all objects respond to this message, whose default behavior is to call the standard NSObject method description.

When you are browsing your workspace, the name of each variable is displayed to the left of the corresponding object description.

Once you have identified the object to which you will send a message, you just need to select it by clicking on it.

Visualizing Methods

When you select an object, the browser populates the column to the right of the object with the list of methods implemented by this object. The methods are grouped according to the class hierarchy for the selected object: first, the methods for the object class, then the methods at superclass level, and so on. In each group, methods are displayed in alphabetical order. In the current version, it is possible for a method to be present in multiple groups, if it has been redefined in subclasses.

Invoking a Method

To invoke a method on the selected object, you just click on the method name. F-Script will then invoke the method and display the result in a new column of the browser.

If the method takes arguments, F-Script will open a window into which you will be asked to add arguments. You provide each argument by typing an F-Script expression (this can be an arbitrary complex expression). In the future, we'd like to enhance this functionality by allowing objects to be dragged and dropped.

Object Browser
Fig 2. The object browser asks for arguments.

If an error occurs during argument evaluation or method invocation, the browser will open an alert panel to inform you of the problem.

Method invocation follows the F-Script rules. Notably, if the result of the Objective-C method you've called is not an object, it will be mapped to an object in accordance with the standard F-Script rules (note: these rules are described into the F-Script manual).

Browsing the Workspace

An F-Script object browser is always associated with an F-Script workspace (a workspace is simply a set of associations between names and objects). You can examine your current workspace and browse objects in it by clicking on the "Workspace" button.

Even if you haven't assigned any objects to identifiers, you may find some predefined objects in your workspace. First, there is the sys object, which is a standard global object used to interact with some of the F-Script run-time services. Second, there may be some arrays containing objects of the Airplane, Flight, and Pilot classes. They are created at launch time, for use by the F-Script Tutorial (you'll find this tutorial in the F-Script guide).

Adding an Entry to the Workspace

You can assign the currently-selected object to a name in your workspace by clicking on the "Name" button, and typing the chosen name in the sheet. This operation is equivalent to the assignment instruction := in the F-Script language.

Object Browser asking for name
Fig. 3. A sheet asks for the name of the object.

Note: if you hit the button by mistake, pressing the Return key on your keyboard without typing anything will dismiss the sheet.

Browsing All Classes

You can browse the classes linked to the executable from which you are using the F-Script object browser (since F-Script is offered as an embeddable framework, you may want to use it from different applications). To start browsing the classes, just click on the "Classes" button.

Browsing classes
Fig. 4. Browsing classes.

You can manipulate classes through the browser in the same way as other objects. This is because Cocoa classes are themselves regular objects. Indeed, each class is itself an instance of another class (called a meta-class) automatically created by the object system, and meta-classes are themselves objects. Since F-Script makes it so easy to browse these different levels, it can be useful to have a clear view of the topology of the Cocoa class system. We illustrate it below, using the Cocoa NSBox class as example.

Topology
Fig. 5. Topology of the Cocoa class system.

A few remarks:

There are a few classes that you can't use with F-Script, and therefore can't browse. This includes classes not conforming to the NSObject protocol (like those inheriting from the "old" Object class), and some special system classes like NSFault and _NSZombie.

Sending the self Message

All Cocoa objects support the self message. This simply returns the receiver of the message. As with all other messages, it can be sent using the object browser in the way we described. But sending this message is (surprisingly) so useful in the context of the object browser that there is a shortcut, in the form of the "self" button. By clicking on it, you send the self message to the selected object.

Sending this message is useful for browsing nested collection, and for getting an up-to-date description of an object.

Inspecting the Selected Object

The "inspect" button opens an inspector for the selected object. A generic inspector is provided and some object offer more specific inspectors (by implementing the inspect method). For instance, inspecting a block will open a code editor for the block.

Browsing Collections

The object browser provides a handy way to browse collections of objects like NSArray, NSSet, or NSDictionary.

When browsing a collection, the browser not only presents the collection as a whole, but also displays and makes selectable each of the elements in the collection. Thus, you can select the collection itself, or directly select one of its elements.

Browsing
Fig. 6. Browsing an NSArray of strings.

When browsing nested collections, sending the self message to an element lets you conveniently delve into nested sub-levels.

The Status Bar

As you move the mouse pointer, the status bar at the base of the browser shows you a more complete description of objects and methods than the one in the columns, which is sometimes limited by the column size.

Status bar
Fig. 7. The status bar is handy!

Update Policy

Objects displayed in the column of the browser or by the generic inspector can be modified at any time in a number of ways: events, callbacks, sending a message with side effect from the object browser or from the F-Script shell, etc.

It is important to note that the browser doesn't automatically update its display when such modification occurs. However, selecting an object and sending it the self message or opening the generic inspector on it will let you see an updated description of the object.

Philippe Mougin specializes in object-oriented technologies and enterprise systems. He is the creator of F-Script, an open-source object-oriented scripting language for Cocoa.


Return to the Mac DevCenter.

Copyright © 2007 O'Reilly Media, Inc.