Browsing Cocoa with F-Scriptby Philippe Mougin
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
The general principle governing usage of the object browser is very simple:
- You select an object by clicking on it.
- A list of all the object's methods is displayed in a new column of the browser.
- You select the method you want to invoke by clicking on it. The browser asks you for parameters, if needed, and invokes the method. The result (an object) is displayed in a new column.
- You can then manipulate the result in the same way.
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.
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:
- The F-Script code
sys browsewill open the browser in your current workspace.
- The F-Script code
sys browse:anObject, in which
anObjectcan be any F-Script expression, will open the browser on
Visualizing and Selecting Objects
|Let's hear the questions and comments you have for Philippe.|
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
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.
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.
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
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).
Pages: 1, 2