Starting A Cocoa-Python Application

Getting started is easy. First, install the Developer Tools if you haven't yet. Now launch Xcode and start a new project. Select "Cocoa-Python Application". You'll be presented with the following window.

xcode project window

Before we begin, go ahead and double-click on "MainMenu.xib (English)" and put together your interface. We're going to make a tag editor, so give yourself a window with:

  1. Text fields for things like: Name, Artist, Album, Track Number, Genre.
  2. Buttons for two actions: Revert and Save & Close.
  3. Menu items for: Open, Save, Revert to Saved, Close. Keep the edit menu, window menu, and help.

If you prefer, use mine. It already as all the needed connections made, the code is all that is missing.

Now we will start the controller. Create a new file for the project of the type "Python NSWindowController subclass". Name it something like "".

To tell the system to actually use our controller code we need to make a quick edit. Our first edit will be to Add the following where the other import statements are:

import controller

Some PyObjC Basics

We're ready to start building our controller. But first let's go over a few basics.

  1. Outlets. If we want to attach values to variables we need to have outlets. You add these as class variables to the controller and assign them the value returned from the function IBOutlet:

    form_field = IBOutlet()
  2. Actions. If we want to attach actions to functions we need to inform the system what methods are eligible. This is done by adding the IBAction decorator to a method:

    def clickButton_(self, sender):
  3. Colons. Objective C loves colons. It uses them to separate return values, method names, and arguments. The method:

     - (IBAction)convert:(id)sender;


    def convert_(self, sender):

    So we simply change colons to underscores. Easy.

    Note: underscores are only legal in function names when they would be colons in Objective C. Using underscores in other cases will cause the runtime to expect too many arguments for a function and fail. This restriction only applies to functions used in the PyObjC runtime bridge, and not pure python.

Part 3: Writing A Python Controller >>

Posted: Jun 11, 2008 | Tags: python osx programming pyobjc cocoa

Comments are closed.


42 | django | python | oggify | OSCON | OSCON07 | osx | utosc | mythtv | security | reading | music | gaming | programming | tips | shell | vim | pyobjc | cocoa | iphone | blog | git


<< Jun >>
Mo Tu We Th Fr Sa Su
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6


This space reserved for a search box

A Django site. Hosted on a Slicehost Slice