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


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.


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


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



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


<window delegate="#NSOwner" resizable="no">
<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"/>

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



Hillegass Example, Ch. 02

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

class AppDelegate(NSObject):
outputNum = None
def windowWillClose_(self, notification):
def quit_(self, notification):
def close_(self, notification):
def applicationDidFinishLaunching_(self, notification):
NSBundle.loadGSMarkupNamed_owner_('MainWindow', self)
def seed(self):
def generate(self):

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

if __name__ == '__main__': main()


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

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.
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?