macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Scripting Cocoa with F-Script
Pages: 1, 2

The currency converter script, version 2

Below is the second version of the currency converter. This script is shorter because we use some neat F-Script features that we avoided in the first version for simplicity's sake. We also removed comments (the code speaks for itself) and reorganized the program a little bit. The interface is now entirely constructed and configured before being put onscreen. Finally, we added support for the Cocoa memory management system to ensure that the various Cocoa objects we create will be destroyed when no longer in use.

F-SCRIPT CURRENCY CONVERTER (version 2)

window := NSWindow alloc initWithContentRect:(125<>513 extent:400<>200) 
                   styleMask:NSTitledWindowMask + NSClosableWindowMask 
                             + NSMiniaturizableWindowMask + NSResizableWindowMask  
                   backing:NSBackingStoreBuffered 
                   defer:NO.

conversionScript := [(form cellAtIndex:2) setStringValue:(form cellAtIndex:0) floatValue 
* (form cellAtIndex:1) floatValue. form selectTextAtIndex:0].
  
form := (NSForm alloc initWithFrame:(60<>90 extent:320<>85)) autorelease.
form addEntry:@{'Exchange Rate per $1','Dollars to Convert','Amount in Other Currency'}.
form setAutosizesCells:YES; 
setTarget:conversionScript; setAction:#value.

button := (NSButton alloc initWithFrame:(250<>20 extent:90<>30)) autorelease.
button setBezelStyle:NSRoundedBezelStyle; 
setTitle:'Convert'; setTarget:conversionScript; setAction:#value.

line := (NSBox alloc initWithFrame:(15<>70 extent:370<>2)) autorelease.
  
window contentView addSubview:@{form, button, line}. 
window setTitle:'Currency Converter'; orderFront:nil.


One of the new things we use in this version is the ; notation for cascading messages. This notation enables us to send several messages to a single receiver without having to re-specify the receiver each time.



Another interesting thing is the instruction:

form addEntry:@{'Exchange Rate per $1','Dollars to Convert','Amount in Other Currency'} 

One of the innovative features of F-Script is that it allows us to manipulate entire groups of objects at once, even with methods that have not been specifically designed to support objects collections (actually, F-Script provides a full object-query language, directly usable on Cocoa objects).

This is the case in this instruction where we add a whole list of entries to the form at once. We use the "message pattern" notation (denoted by @) that allows us to specify potentially complex groups of message sends. A message pattern generally involves single or multiple collections of objects: In our example, we use an array of string objects, denoted by { and }. At runtime, the instruction will trigger the generation of these three message sends:


form addEntry:'Exchange Rate per $1'
form addEntry:'Dollars to Convert'
form addEntry:'Amount in Other Currency'

The same pattern is also used in the instruction "window contentView addSubview:@{form, button, line}" where we put a whole set of views into the window at once.

In these examples, we use a relatively simple pattern. F-Script provides a complete syntax that makes it possible to express a broad range of message patterns. All the specific concepts of F-Script, like message patterns, can be used when scripting Cocoa.

The currency converter script, version 3

Comment on this articleNow that you've had a chance to explore F-Script, let us know what you think. Also, if you have any questions for Philippe, he'd be glad to respond.
Post your comments

As it stands, our script is not very modular: It's just a set of instructions, using global variables, that we paste into the F-Script console in order to execute them. The following version introduces modularity:

  • Our script will become an object itself.
  • It will now use local variables instead of global variables, so that we won't pollute the top-level environment.
  • It will take the window's title as the argument.

F-SCRIPT CURRENCY CONVERTER (version 3)


converter := [:title | |window conversionScript form button line|
  window := NSWindow alloc initWithContentRect:(125<>513 extent:400<>200) 
                     styleMask:NSTitledWindowMask + NSClosableWindowMask 
                               + NSMiniaturizableWindowMask + NSResizableWindowMask  
                     backing:NSBackingStoreBuffered 
                     defer:NO.

  conversionScript := [(form cellAtIndex:2) setStringValue:(form cellAtIndex:0) 
  floatValue * (form cellAtIndex:1) floatValue. form selectTextAtIndex:0].
  
  form := (NSForm alloc initWithFrame:(60<>90 extent:320<>85)) autorelease.
  form addEntry:@{'Exchange Rate per $1', 'Dollars to Convert', 'Amount in Other Currency'}.
  form setAutosizesCells:YES; setTarget:conversionScript; setAction:#value.

  button := (NSButton alloc initWithFrame:(250<>20 extent:90<>30)) autorelease.
  button setBezelStyle:NSRoundedBezelStyle; setTitle:'Convert';
setTarget:conversionScript; setAction:#value.

  line := (NSBox alloc initWithFrame:(15<>70 extent:370<>2)) autorelease.
  
  window contentView addSubview:@{form, button, line}. 
  window setTitle:title; orderFront:nil.
]

In this version, we put the instructions between [ and ] to make the F-Script interpreter generate a Block object. We also declare, on the first line of the script, the argument and the local variables. Finally, we give the name "converter" to the Block object that represents our script.

We can now invoke our script by sending it a value message without forgetting to provide the required argument. For instance:


converter value:'This converter is nice!'

Each time we invoke it, a new, fully functional currency converter is created and displayed onscreen.

Because our script is now an object of class Block, we can manipulate it like any other object: put it in a collection, pass it as an argument to methods, archive it on disk, and more. There are also several facilities specifically related to Block objects, including a graphical code editor.

Finally, because F-Script comes in the form of a framework ready to be embedded into any Cocoa application, it is easy to place our script in a standard Mac OS X executable.

Final thoughts

We have seen what scripting Cocoa with F-Script involves. Clearly, many subjects have not been tackled in this article, such as Cocoa exception handling, object archiving, Interface Builder integration, usage of custom Objective-C classes, or mapping of non-object types, but you can expect a very high level of integration on these aspects.

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.