Building Cocoa GUIs in Python with PyObjC, Part Four
Creating an Open Dialog
Our controller doesn't really do anything at this point. We'll begin by adding an open dialog for the user. This will require us to really delve into the Objective C bridge provided by PyObjC. We'll start by reading some documentation that ships with Xcode.
Let's launch the local documentation browser in Xcode. Under "Help" click "Documentation". In the newly opened documentation browser select the "Mac OS X 10.5" documentation set and search for NSOpenPanel. This is the class we'll be working with to create our open dialog. Have a look through the documentation, then let's code.
First we do some basic stuff, creating the object and setting some permissions. A modified open method on our controller.py looks like this:
filetypes = ('mp3', 'ogg', 'mp4', 'flac', 'm4a', 'm4p')
@IBAction
def open_(self, sender):
panel = NSOpenPanel.openPanel()
panel.setCanChooseDirectories_(NO)
panel ...
Building Cocoa GUIs in Python with PyObjC, Part Three
Writing A Python Controller
First let's add your new controller into the GUI. Add an object from "Objects & Controllers" into your GUI. Now click on it and bring up the "Identity" tab in the inspector. The first drop-down lets you select a class, find "controller" and pick it.
Now Open up your newly created controller.py with your favorite text editor, and we'll begin.
#
# controller.py
# QuickTag
#
# Created by Scott Paul Robertson on 6/11/08.
# Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
from objc import YES, NO, IBAction, IBOutlet
from Foundation import *
from AppKit import *
class controller(NSWindowController):
pass
There are two parts we will be adding, the Outlets (variables) and Actions (methods). first lets add a few outlets to the class controller.
name = IBOutlet()
artist = IBOutlet()
albumArtist = IBOutlet()
album = IBOutlet()
...
These class variables can now be connected to various fields in your GUI. In Interface Builder ...
Drafts Do Not Go Into The Feed
Remember kids, do not feed your feed drafts. It is unprofessional.
Blog updated to filter out drafts in the RSS feed.
Building Cocoa GUIs in Python with PyObjC, Part Two
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.
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:
- Text fields for things like: Name, Artist, Album, Track Number, Genre.
- Buttons for two actions: Revert and Save & Close.
- 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 "controller.py".
To tell ...
Building Cocoa GUIs in Python with PyObjC, Part One
Introduction
Building GUIs for Apple OS X traditionally meant you would code in Objective C. To overcome this issue people have made programming bridges to allow development in other languages. PyObjC is the project that enables Python programmers to take advantage of Cocoa, Apple's development environment. I recently began learning how to use PyObjC, and how to make (almost) pure Python GUI applications.
PyObjC isn't new, it has been around for a while, and there is actually a pretty good tutorial for wiring an interface up with Python. Leopard (10.5) ships with Python 2.5 and PyObjC 2.0, meaning there is nothing we need to install. Additionally Apple has also shipped support for Python in Xcode. This makes certain things much easier.
The current tutorial directs a user to build an interface, and then take the generated Nib file* and run a script that generates the ...
Yet More Blog Upgrades
Well I've added a few more improvements to the blog software.
- Drafts can now be viewed on a login restricted url.
-
The
READMEfile now has details of template tags, an exampleblog.html, and the CSS used. - Improved doc strings so you can read template tag info in the admin.
There are other things I'd like to see, but this works well for now.
Updates to the Blog Code
I've made some updates to the software that runs this blog. A few changes that make for good reasons to update:
-
The subtitle block no long adds the <h2> tag. You'll need to adjust your
blog.htmlto put tags around the subtitle block. -
Fixed the way it references your settings. I'm now doing it correctly by writing
from django.conf import settings. - Some minor other cleanup.
Share and Enjoy.
Oggify 2.0 rc 2
Just a version bump of Oggify to RC 2 (tar). Re-encoding works again.
Also, if anyone has time, let me know in the comments if the standard python setup.py install worked for you with Oggify. Mention you distro, etc if you could.
Oggify 2.0 rc 1
I'm announcing the release of Oggify version 2.0 release canidate 1 (tar). New features: 0. I have added a small number of unit tests, which should provide more stability with updates. Additionally I've done a huge refactor of the design, which will make things much easier for me. One feature remains: re-tagging. I'm think that'll be rc 2.
Full Code Navigation: Cscope
Not long ago we discussed ctags here, and how it can quickly let you navigate your code, helping you find definitions of variables and functions. You'll have taken notice that sometimes you want to navigate in the reverse: where is this function called, who includes this file, etc. Cscope builds a database of such information (and more). We can include it with ctags and have very impressive code searching.
If you want a detailed introduction, there is a great cscope and vim tutorial available. I'll just give you a quick run down of using it.
At the base of your C or C++ source tree run
cscope -R -b -qAdd this to your
.vimrc:if has("cscope") set cst set csto=1 if filereadable("cscope.out") cs add cscope out endif endifStart Vim in the same directory as the generated
cscope.outfile. (If not, just ...
Host Aliases in SSH
So you've probably got one or two hosts that you frequently ssh to that have long hostnames. You'd prefer to just alias this to something really short. There's a couple ways to do this:
- Hosts file alias. Problem: may overwrite something useful for other network connections.
- Shell alias. Problem: too many aliases.
- Good shell tab-completion. Problem: I can never get zsh to do intelligent ssh host completion.
- SSH configured alias.
So to create an alias for a system add to ~/.ssh/config the following:
Host <alias>
HostName <real system fqdn>
Some examples:
Host s
HostName scottr.org
Host b
HostName 192.168.1.1
After a Host entry can be host specific configuration, and Host can be a pattern (Host *.slashdot.org). So for a host you could disable host key checking, or use a specific key file. Comes in pretty handy.
Slicehost - Great Hosting
I don't think I've really mentioned my hosting provider, Slicehost on here before, but they deserve a bit of mention for providing great service. Be warned, the rest of this message might be construed as a shameless promotion.
They provide a Xen based VPS solution. I've been using them for about a year and a half now. I originally moved to them due to the excellent price to RAM/HD/Processor ratio. Since then they've added a host of handy features (not all features listed are new):
- DNS hosting (with a zone editor that doesn't suck)
- AJAX based web console to system
- Cheap, automatic, backups
- Great statistics/monitoring
- An excellent wiki, blog, and articles*
- Constant updates on service issues from their blog or twitter
- I'm told 24/7 problem resolution (but I haven't had any)
- Two data-centers
I've been extremely happy with ...
Navigating Man Pages in Vim
If you program in C you probably look to the man pages as a prime reference. There are a lot of different functions in the standard library. For example, what does this all mean?
sscanf(str, "%*d: %12[^a-zA-Z] 0x%4x %n", ...)
Luckily in Vim I can just press K over sscanf and in a window split I'll have the man page. Now this isn't a new feature, except that the man page opens in a Vim window not in a pager. This is a feature enabled by enabling a plugin that ships with Vim.
As an additional bonus the man page has syntax highlighting and is tagged so you can travel between man pages with CTRL-] and CTRL-T. Now when you see the references (like getc(3)) you can just scroll over and hit CTRL-] to view it.
To enable this plugin, and map K to use ...
ctags and Vim
At work I deal with a large code base. Taking into consideration that I'm often still familiarizing myself with the code, you can imagine that it isn't easy finding things. An average day working in the code used to involve me trying to determine what a bit-field flag means, which involves tracking down a #define somewhere in the code. Worse yet are the various functions I've seen around and now need to understand what they're doing, leaving me to find where they're doing it.
Now at first I was running commands like egrep -R '^some_function_name' * or egrep -R '^#define \+FLAG' *. This usually worked, but it meant I would then have to then open the file up in my vim session. This took away a lot of time, and I knew that there was a better way.
ctags is a program that will go through code ...
Oggify 2.0 Beta 4
Oggify is now on Beta 4. This version covers a few important bugs including:
- Infinite loops with symlinks
- MP3's not getting comments set in the tag
So go ahead and download the tar file.

