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 looks like this:

filetypes = ('mp3', 'ogg', 'mp4', 'flac', 'm4a', 'm4p')

def open_(self, sender):
    panel = NSOpenPanel.openPanel()

This gives us an NSOpenPanel object, turns off directory selection and multiple selections. The filetypes tuple contains the extensions that we allow the user to select, which will be used when we start the dialog.

The first open dialog we'll write will be modal. Modal dialogs block other actions in the application until the window finishes. It is the easiest way to do an open dialog. Have a look at the function runModalFortypes: in the documentation. Now to add to the open method:

    ret_value = panel.runModalForTypes_(self.filetypes)
    if ret_value:
        print "Open %s" % panel.filenames()
        print "Canceled"

Go ahead and run this code.

open window

A few things to note:

Since our app really doesn't need to take up so much screen real estate we'll use a sheet instead. This is attached to the window that creates it, and looks a whole lot cooler. Unfortunately it is more difficult to use. Have a look at beginSheetForDirectory:file:types:modalForWindow:modalDelegate:didEndSelector:contextInfo: in the documentation. Now put in this code in place of what we just added:

            os.getcwd(), None, self.filetypes, NSApp().mainWindow(),
            self, 'openPanelDidEnd:panel:returnCode:contextInfo:', 0)

We'll need a new method on the class controller:

def openPanelDidEnd_panel_returnCode_contextInfo_(self, panel, returnCode,
    if returnCode:
        print "Open: %s" % panel.filenames()
        print "Cancel"

Run this and you'll see the lovely open sheet that behaves like our previous modal dialog.

open sheet

Items of note:

By looking through the official documentation on NSOpenPanel and this you should be able to get a very good feeling for how to translate between Objective C and PyObjC. The code for this step is available in git as always.

In the next installment we'll get QuickTag reading and writing audio tags, finishing the controller.


<< Part3: Writing A Python Controller | Part5: Adding Python Modules to the Bundle >>

Posted: Jul 04, 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


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


This space reserved for a search box

A Django site. Hosted on a Slicehost Slice