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

Continue Reading

Permalink | 1 comment | Posted: Jul 04, 2008 | Tags: python osx programming pyobjc cocoa

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

Continue Reading

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

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.

Permalink | Posted: Jun 12, 2008 | Tags: django python programming

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.

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 "controller.py".

To tell ...

Continue Reading

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

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

Continue Reading

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

Yet More Blog Upgrades

Well I've added a few more improvements to the blog software.

  1. Drafts can now be viewed on a login restricted url.
  2. The README file now has details of template tags, an example blog.html, and the CSS used.
  3. 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.

Permalink | Posted: May 20, 2008 | Tags: django python

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:

  1. The subtitle block no long adds the <h2> tag. You'll need to adjust your blog.html to put tags around the subtitle block.
  2. Fixed the way it references your settings. I'm now doing it correctly by writing from django.conf import settings.
  3. Some minor other cleanup.

Share and Enjoy.

Permalink | Posted: May 19, 2008 | Tags: django python

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.

Permalink | 1 comment | Posted: Apr 27, 2008 | Tags: python oggify

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.

Permalink | Posted: Apr 23, 2008 | Tags: python oggify

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.

  1. At the base of your C or C++ source tree run cscope -R -b -q

  2. Add this to your .vimrc:

    if has("cscope")
      set cst
      set csto=1
      if filereadable("cscope.out")
        cs add cscope out
      endif
    endif
    
  3. Start Vim in the same directory as the generated cscope.out file. (If not, just ...

Continue Reading

Permalink | Posted: Apr 18, 2008 | Tags: programming tips vim

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:

  1. Hosts file alias. Problem: may overwrite something useful for other network connections.
  2. Shell alias. Problem: too many aliases.
  3. Good shell tab-completion. Problem: I can never get zsh to do intelligent ssh host completion.
  4. 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.

Permalink | 2 comments | Posted: Apr 18, 2008 | Tags: tips shell

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):

I've been extremely happy with ...

Continue Reading

Permalink | Posted: Mar 25, 2008 | Tags: 42

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

Continue Reading

Permalink | Posted: Mar 21, 2008 | Tags: programming tips

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

Continue Reading

Permalink | 2 comments | Posted: Feb 24, 2008 | Tags: programming tips

Oggify 2.0 Beta 4

Oggify is now on Beta 4. This version covers a few important bugs including:

So go ahead and download the tar file.

Permalink | Posted: Feb 14, 2008 | Tags: oggify

Tags

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

Calendar

<< Jul >>
Mo Tu We Th Fr Sa Su
30 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 31 1 2 3

Search

This space reserved for a search box

RSS Feeds

A Django site. Hosted on a Slicehost Slice