Painlessly add a Nu console to an existing Cocoa application

4 views
Skip to first unread message

Jeff Buck

unread,
Mar 19, 2008, 1:55:43 PM3/19/08
to Programming Nu
I wanted to see how painlessly I could add a permanent Nu console to
an existing Cocoa application (in code, without manually using
NuAnywhere). It turned out to be really easy. All of the needed
pieces of code were on the Nu website or in the example Nu
applications. I just whittled them down to a few lines in a single .m
file.

First, add Nu.framework to your project.

There are a few places you could add the Nu console startup code in
your Cocoa application. I put the code at the end of the awakeFromNib
method of my main controller class:

// At top of .m file:
#import "Nu/Nu.h"

// ...

- (void)awakeFromNib
{
// ...

NSString* consoleStartup =
@"(progn \
(load \"console\") \
(set $console ((NuConsoleWindowController alloc) init)))";

id parser = [Nu parser];
id code = [parser parse:consoleStartup];
id result = [parser eval:code];
}

Compile and run your application. Command-L will bring up the Nu
console. If you want the Nu console to automatically appear on
application startup, add
($console toggleConsole:nil)
as the last line of the progn in the consoleStartup string above.

Now, start exploring:

> (((NSApplication sharedApplication) mainWindow) firstResponder)
<LifeView:478240>
> (set lv (((NSApplication sharedApplication) mainWindow) firstResponder))
<LifeView:478240>
> (set lc (lv lifeController))
<LifeController:453010>
> ((lc instanceMethods) each: (do (method) (puts (method name))))
awakeFromNib
changeSpeed:
clear:
gridChangeRows:columns:
gridChangeWrap:
init
life
loadGliderGun:
loadPattern:
randomize:
showPreferences:
singleGeneration:
start:
stop:
timerUp:
toggleCellAtX:y:
toggleRunning:
view
windowShouldClose:
<NSCFArray:14831090>
>

I used F-Script a lot in the past, but I can't say that I ever warmed
up it's syntax. Nu in a console feels like home.

-Jeff




Dean Mao

unread,
Mar 19, 2008, 8:57:41 PM3/19/08
to program...@googlegroups.com
Nice! I'm going to have to try this out later tonight!
Reply all
Reply to author
Forward
0 new messages