Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [164]

By Root 1011 0
familiar with JavaScript define some of what happens in your application.

This is something you’re most likely to use in conjunction with a web view. Using the WebView class from WebKit, you can “reach through” to a WebScriptObject, which lets you call JavaScript functions from Objective-C, and also lets you expose an Objective-C object to the JavaScript interpreter, so that the JavaScript code can call Objective-C methods, using a method-to-function mapping similar to what we described for PyObjC. This is primarily useful if you want to use a WebView to lay out your GUI, but still include controls in your GUI that can affect things outside of the javascript environment.

But wait, there’s more! Besides the JavaScript support that WebKit gives you, there’s a third-party option called JSCocoa, which fully bridges JavaScript with Cocoa, using the same JavaScript engine (JavaScriptCore) that’s a part of WebKit. This lets you write complete Cocoa applications in JavaScript, much like you can in Python or Ruby. One nice feature of JSCocoa, which it shares with Cappuccino (described in the final section of this chapter) is that, in addition to the translation between JavaScript-style function names and Objective-C method names, JSCocoa actually lets you write your javascript in a syntax that is hauntingly similar to Objective-C:

JSCocoa:

That’s right, the only difference is the absent semicolon, because you don’t need a semicolon at the end of a line of JavaScript if the end of the line is also a valid statement ending. We’re the first to admit that this seems like a JavaScript miracle: we don’t have the JavaScript knowledge to have the slightest idea how this trick was pulled off, and probably never will, but that’s okay. If you want to learn more about this, http://inexdo.com/JSCocoa is the place to look.

F-Script


No discussion of alternate languages for programming in Cocoa would be complete without mentioning F-Script. Unlike most of the language bridges, which are ultimately meant to let you build Cocoa apps in a different language, F-Script seems to have a couple of complementary purposes. First of all, you can embed F-Script into your applications, providing an easy way to add user scripting to your apps, including GUI components for entering and trying out scripts written in the F-Script language, which is based on Smalltalk and therefore pretty similar to Objective-C.

The other, perhaps even more useful features of F-Script are the tools it provides to you, the developer, in the form of an interactive command shell that lets you explore your running application. This includes a graphical browser that lets you navigate among the objects in your application, inspecting their properties, traversing relationships to other objects, and so on. It’s like taking the interactivity you have in gdb, and turning it up to 11.

Not only that, you can also inject F-Script into any running Cocoa application, and use the same tools to explore its objects! Although this functionality is not as useful as having another app’s source, being able to browse the structure of other applications can be highly educational, and fun as well. This is really a tool that all Cocoa developers should have some familiarity with. Run, don’t walk, to http://www.fscript.org to try this out for yourself.

Ported Cocoa


This book is primarily about making Mac software (it’s right there in the title), but we’d be doing you a disservice if we didn’t point out the other areas where Cocoa and Cocoa-ish technologies may be applied.

Cocoa Touch


These days, with iPhone development growing by leaps and bounds, presumably everyone reading this book knows a bit about the iPhone development model, and the fact that it’s based around Objective-C. In fact, it’s a pretty safe bet that you probably did some iPhone development before testing the Mac waters. In spite of that, it’s worth mentioning the iPhone’s Cocoa Touch frameworks, and how it differs from the Cocoa frameworks we’ve been covering.

Cocoa’s core classes are split between

Return Main Page Previous Page Next Page

®Online Book Reader