A program is a process, not a thing. This also applies to life, the universe, and everything.

2004-11-05

Small Nerd Example Chapter 2

Aaron Hillegass' book, "Cocoa Programmig for Mac OS X" is a great way to learn Cocoa, Objective-C and Interface Builder, and I highly recommend you buy it to understand OS X programming (of course, you'll be buying the new, improved Second Edition, and I'm working off the first edition here, so things may not completely match up). In order to demonstrate how compact, yet readable, Renaissance + Python can be, I'm going to convert the example programs from this book. Aaron's company is called Big Nerd Ranch, and I was trying to find something to show which postings were examples from the book, so readers can follow along, but without implying in any way that Aaron has approved of these conversions in any way, so I'm going to call them the Small Nerd Examples (The small nerd being me).

Note that while you are developing, it is inconvenient to have to build the project every time you touch a file, so you can use python setup.py py2app --alias to build it once, then you can edit your files normally and the changes will be reflected when you run your application. Just remember to re-run this when you add a new file, and to re-run it without the alias flag when you're building any version to be distributed.

Like the previous example, Hello World, this one contains four files. The setup.py file will look radically similar to the Hello World version. The program itself is simple: A window with two buttons, one which seeds the random number generator with the current time, and another which generates and displays a random number between 1 and 100 in a text field.

MainMenu.gsmarkup

<?xml version="1.0"?>
<!DOCTYPE gsmarkup>
<gsmarkup>

<objects>

<menu type="main">
<menu title="ch02" type="apple">
<menuItem title="About ch02" action="orderFrontStandardAboutPanel:"/>
<menuSeparator/>
<menu title="Services" type="services"/>
<menuSeparator/>
<menuItem title="Hide ch02" action="hide:" key="h"/>
<menuItem title="Hide Others" action="hideOtherApplications:"/>
<menuItem title="Show All" action="unhideAllApplications:"/>
<menuSeparator/>
<menuItem title="Quit ch02" action="terminate:" key="q"/>
</menu>
<menu title="Window" type="windows">
<menuItem title="Minimize Window"
action="performMiniaturize:" key="m"/>
<menuSeparator/>
<menuItem title="Bring All to Front"
action="arrangeInFront:"/>
</menu>
</menu>
<menu title="Help" type="help">
<menuItem title="ch02 Help" keys="?"/>
</menu>
</objects>

</gsmarkup>


MainWindow.gsmarkup

<?xml version="1.0"?>
<!DOCTYPE gsmarkup>
<gsmarkup>

<objects>

<window delegate="#NSOwner" resizable="no">
<vbox>
<button target="#NSOwner" action="seed"
title="Seed random number generator with time"/>
<button target="#NSOwner" action="generate"
title="Generate random number"/>
<textField editable="no" id="text" align="center"/>
</vbox>
</window>

</objects>
<connectors>
<outlet source="#NSOwner" target="text" key="outputNum"/>
</connectors>

</gsmarkup>


ch02.py

'''
Hillegass Example, Ch. 02

'''
from Foundation import *
from AppKit import *
from Renaissance import *
import random

class AppDelegate(NSObject):
outputNum = None
def windowWillClose_(self, notification):
NSApp().terminate_(self)
def quit_(self, notification):
NSApp().terminate_(self)
def close_(self, notification):
NSApp().terminate_(self)
def applicationDidFinishLaunching_(self, notification):
NSBundle.loadGSMarkupNamed_owner_('MainWindow', self)
def seed(self):
random.seed()
def generate(self):
self.outputNum.setStringValue_(str(random.randint(1,100)))

def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
NSBundle.loadGSMarkupNamed_owner_('MainMenu', delegate)
NSApp().run()

if __name__ == '__main__': main()


setup.py

'''
Run with:
% python setup.py py2app
or
% python setup.py py2app --alias # while developing
'''
from distutils.core import setup
import py2app

setup(
data_files = ['MainMenu.gsmarkup', 'MainWindow.gsmarkup'],
app = ['ch02.py'],
)


OK, this time out we've got about 52 lines of XML markup and 44 lines of Python. Of course, the program doesn't really do much more than Hello World, but we're getting somewhere. If anyone has questions about the code, or want more explanatory text around the Renaissance markup, please let me know in the comments or by email.
Comments:
This is great. I hope you keep up with this series.
 
Thanks for the kudos, I certainly intend to keep going with this. The intersection of OS X, Python, and Renaissance is the most exciting thing I've seen in years.
 
Your front page shows two comments. The actual article page shows zero.
 
Scrub my last comment. Wow, I finally have reason to believe the OmniWeb cache actually does something...
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?