Sometimes you sit there with a headache and the memory of an article and two e-mails you read two days ago, not being able to do much else, and you suddenly comprehend something you didn’t before. And if the headache isn’t from alcohol, you can actually assume that this revelation will still be valid once you feel human again. Try this one:
There were a couple of postings on Cocoa-Dev by someone trying to use Cocoa APIs using AppleScript Studio’s call method function. Now, if you remember, that function lets people used to not-quite-English statements like
copy (display dialog with prompt "I failed jiggery-pokery but I had A's in hullaballoo") to myVar
turn nice readable Objective C statements like
foo = [[NSFileManager defaultManager] directoryContentsAtPath: @"/Users/"];
into toll-free-bridged unreadabilities like
set foo to (call method "directoryContentsAtPath:" of (call method "defaultManager" of class "NSFileManager") with parameter "Macintosh HD:Users")
(Note that I inserted the brackets in the AppleScript to improve readability).
Now, when I first read this I thought that guy got what he deserved. If you clicked the link above, you’ll know my thoughts about AppleScript, and this example shows beautifully why you want to do Cocoa development in Objective C. So why doesn’t that guy bite the bullet and switch languages?
Then, today, I wrote an e-mail to Tom Pittman and in the last line gushed a little about how his CompileIt! stack helped me get into programming from within the safety of HyperCard. Now, for those of you who never got to play with HyperCard or CompileIt!, basically CompileIt is like AppleScript Studio’s call method command: It lets you call all the system APIs as if they were HyperTalk handlers and functions. So to have Quickdraw draw a rectangle, you’d write something like:
put newPtr(8) into myRect -- 8 bytes, for 4 two-byte shorts setRect myRect,10,10,100,100 frameRect myRect disposPtr myRect -- yes, no "e" - those were the System 6 symbol names.
Of course, you could have just done this in plain HyperTalk, by scripting the user’s actions with the drawing tools, like:
choose rect tool drag from 10,10 to 100,100
but play along there, will you? Well, anyway, just like with ASS and call method, this allows you to turn a straight affair of English-like code into a bastardised form of some systems-programming language.
So, what’s the benefit? The benefit is a shallower learning curve. With tools like HyperCard or ASS, you can create the basics of an application, all the drudgework, using a much more natural set of metaphors. And when those fail you, you don’t have to start from scratch writing event loops and window-management code. You just learn about those few lower-level Toolbox or Cocoa commands you need and only use those.
The advantage is that you can get to know the frameworks step by step, having small successes each time. By the time you have to learn the actual details of Cocoa programming, you’ll already know most of the commands and conventions, and it won’t feel half as foreign as it would have if you’d jumped straight in.
It doesn’t change my opinion that AppleScript is a bass-ackwards language, and it doesn’t change my opinion that call method‘s main effect is making code unreadable, but at least it makes me frightened of all these AppleScripters that will push into the Mac programming market eventually once they’ve moved to a better-designed language. They had the same basic learning curve I had, and they put up with AS… They’ll smoke me in their pipes… :-)