Copyright ©1999 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: NSWindowLet'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.
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:
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]
(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]
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: NSSavePanelWe 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]
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]
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"]
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]
The next section shows what you can do with the Joy interpreter besides sending messages to objects. |