Copyright ©1998 by AAA+ Software Forschungs- und Entwicklungs Ges.m.b.H.  All Rights Reserved.

2 Exploring Mac OS X With Joy





This section explains how you can use the Joy interpreter to interactively explore the Mac OS X API. It assumes that you have loaded the Joy palette into Interface Builder (as explained in the previous section).

By default, Joy uses JavaScript as a base language. If you want to use Tcl instead, you have to switch languages using the Preferences menu item in the Info submenu of InterfaceBuilder's Joy menu.

First exploration with Joy: NSWindow

Let's now start to create a New Application which already contains a Joy interpreter by selecting the New Joy Application command in InterfaceBuilder's Joy menu. This will create a new UNTITLED application with a standard window and a default menu.


Figure 1: A new UNTITLED application containing a JavaScript interpreter.

We will now interactively play around with the NSWindow API. We already have a window in our application (called My Window), we only have to tell the Joy interpreter to give it a Joy name:

  1. Ctrl-Drag from the Interpreter object to the MyWindow Object inside the nib document window. This will bring up the interpreter's own connections inspector.


    Figure 2: Connecting the Interpreter with the Application's window.

  2. Click the Connect button
  3. Type in a variable name for the NSWindow object. Call it window. Press Enter.


    Figure 3: The Interpreter's Connections Inspector.

You should now be able to start your application and its Joy interpreter: Choose Test Interface in InterfaceBuilder's Document menu and then choose Command Window... in your application's Joy menu. The interpreter window will appear and now the real joy starts: You can send Objective-C messages directly to your window and see what happens.

Type

    js> [window miniaturize: nil]

and your window gets miniaturized. Of course you can deminiaturize it by double-clicking on the mini-window icon. You can replay this sequence by pressing cursor up and Enter in the Joy window. You can deminiaturize the window by typing:

    js> [window makeKeyAndOrderFront: nil]

You can close an NSWindow with the performClose: method:

    js> [window performClose: nil]

But you can still reopen it as before (press cursor up twice):

    js> [window makeKeyAndOrderFront: nil]

Changing the window's title is very easy, too. Converting a JavaScript string to an NSString object is done automatically, therefore the Objective-C @"..." syntax is not needed (but still allowed):

    js> [window setTitle: "Apple is great!"]

Note that this worked immediately without needing to redisplay the window.

Converting an NSString to a JavaScript string is done automatically, too:

    js> [window title]
    Apple is great!

(As an exercise, you may want to change the title of the miniaturized window to something shorter.)

If you use a method which is not recognized, you get an error message:

    js> [window setColor: 1]
    Objective-C NSInvalidArgumentException: *** -[NSWindow setColor:]: selector not recognized

If you look into the documentation (try help NSWindow) or use Joy's run-time browser (see part 5 of this tutorial for details), you'll find out how the method we are looking for really is named:

    js> [window setBackgroundColor: 1]

Nothing happens, because the setBackgroundColor: message expects an NSColor object (the JavaScript number 1 was converted to an NSNumber object). If the window gets redisplayed (e.g. after deminiaturizing) you may run into serious troubles!

So this is how it works:

    js> [window setBackgroundColor: [NSColor whiteColor]]

Or does it? Try sending the window a display message.

    js> [window display]

You may want to try the same with a more fancy color (maybe orange?).

Second exploration with Joy: NSSavePanel

We now demonstrate usage of a save panel. We will use the same application as before.

The class we need to use to open a save panel is NSSavePanel.  To find out how to access a save panel object, use the help facility by typing help NSSavePanel, or use Joy's run-time browser (see part 5 of this tutorial).  From this you should see that sending the message savePanel will yield a shared instance.  You can execute this by typing

    js> [NSSavePanel savePanel]
    <NSSavePanel: 0x676474>

You can see that the instance is shared by using the up arrow key, and then the enter key, to reexecute the expression. As the instance will be the recipient of many messages, it makes sense to store it in a variable.  This can be done with the command:

    js> sp = [NSSavePanel savePanel]
    <NSSavePanel: 0x676474>

So how can we display the NSSavePanel? The same way as an NSWindow? (NSSavePanel is an descendant of NSWindow in the class hierarchy, so it should recognize the same methods.)

    js> [sp makeKeyAndOrderFront: nil]

Yes, it's there. But it does not display anything. And how does it go away again? If you try NSWindow's performClose: method it will not work because the panel has no close box. We need NSWindow's close method, which does the same as performClose: but without simulating a mouse click (just removing the window from the screen). You may want to try out the difference with the application's default window (you will only see the difference if you have fast eyes or a slow computer).

    js> [sp close]

If you want, you can redisplay the save panel and try other NSWindow methods you are already familiar with (e.g. setTitle:).

If you look into the Yellow Box developer documentation, you will learn the existence of the runModal and the runModalForDirectory:file: methods. Let's have a go:

    js> [sp runModalForDirectory: "/tmp" file: "tempfile"]
    0

You can't type anything into the interpreter now until you click on OK or Cancel in the save panel (that's what modal means!). Note that this method returns 1 if you click on OK and 0 if you click on Cancel. Try what happens if you select an existing file!

You can find out the name of the selected file with the filename method:

    js> [sp filename]
    /Local/Users/joy/newfile

The next section shows what you can do with the Joy interpreter besides sending messages to objects.