views:

268

answers:

2

I'm wanting to use an NSOpenPanel for an application I'm designing. Here's what I have so far:

@objc.IBAction
def ShowOpenPanel_(self, sender):
    self.panel = NSOpenPanel.openPanel()
    self.panel.setCanChooseFiles_(False)
    self.panel.setCanChooseDirectories_(True)
    NSLog(u'Starting OpenPanel')
    self.panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
        self.defaults.objectForKey_(u'projpath'), 
        objc.nil, 
        objc.nil, 
        self, 
        objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_, 
            signature='v:@ii'),
        objc.nil)
    NSLog(u'OpenPanel was started.')

def OpenPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
    NSLog('Panel ended.')
    if (returnCode == NSOKButton):
        NSLog(u'User selected OK')
        path = self.panel.filenames()[0]
        self.defaults.setObject_forKey_(path, u'projpath')
    del self.panel

The main two lines I'm concerned about are:

        objc.selector(self.OpenPanelDidEnd_returnCode_contextInfo_, 
            signature='v:@ii'),
        objc.nil) #this is the argument that gets passed as the void pointer

The third argument is supposed to be a void pointer. Since I don't intend to use that data, I'd rather just leave it empty. I've tried making the signature 'v:@iv' and tried using objc.NULL and python's None, and just about every combination of all these things. What is the best way to handle this?

+1  A: 

I think you don't need to use objc.selector at all; try this instead:

@objc.IBAction
def ShowOpenPanel_(self, sender):
    self.panel = NSOpenPanel.openPanel()
    self.panel.setCanChooseFiles_(False)
    self.panel.setCanChooseDirectories_(True)
    NSLog(u'Starting OpenPanel')
    self.panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
        self.defaults.objectForKey_(u'projpath'), 
        objc.nil, 
        objc.nil, 
        self, 
        self.OpenPanelDidEnd_returnCode_contextInfo_,
        objc.nil)
    NSLog(u'OpenPanel was started.')

I've also found that I need to decorate the end-of-panel function with PyObjCTools.AppHelper.endSheetMethod:

@PyObjCTools.AppHelper.endSheetMethod
def OpenPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
    NSLog('Panel ended.')
    if (returnCode == NSOKButton):
        NSLog(u'User selected OK')
        path = self.panel.filenames()[0]
        self.defaults.setObject_forKey_(path, u'projpath')
    del self.panel

Here's how I would write what you have:

@objc.IBAction
def showOpenPanel_(self, sender):
    panel = NSOpenPanel.openPanel()
    panel.setCanChooseFiles_(False)
    panel.setCanChooseDirectories_(True)
    NSLog(u'Starting openPanel')
    panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
        self.defaults.objectForKey_(u'projpath'),    #forDirectory
        None,    #file
        None,    #types
        self,    #modelessDelegate
        self.openPanelDidEnd_returnCode_contextInfo_,    #didEndSelector
        None)    #contextInfo
    NSLog(u'openPanel started')

@PyObjCTools.AppHelper.endSheetMethod
def openPanelDidEnd_returnCode_contextInfo_(self, panel, returnCode, context):
    NSLog(u'Panel ended')
    if returnCode != NSOKButton:
        return
    NSLog(u'User selected OK')
    path = panel.filenames()[0]
    self.defaults.setObject_forKey_(path, u'projpath')

Explanation of changes: I always use None rather than objc.nil and it hasn't messed me up yet; I don't think your panel needs to be a property of self since you get it in your return function; objc convention is to have the first letter of your function in lower case.

Dan
Thanks, I'll try it out later this week!
Jason Baker
+1  A: 

The right way to open the panel is:

@objc.IBAction
def showOpenPanel_(self, sender):
    panel = NSOpenPanel.openPanel()
    panel.setCanChooseFiles_(False)
    panel.setCanChooseDirectories_(True)
    NSLog(u'Starting openPanel')
    panel.beginForDirectory_file_types_modelessDelegate_didEndSelector_contextInfo_(
        self.defaults.objectForKey_(u'projpath'),    #forDirectory
        None,    #file
        None,    #types
        self,    #modelessDelegate
        'openPanelDidEnd:returnCode:contextInfo:',    #didEndSelector
        None)    #contextInfo
    NSLog(u'openPanel started')

Dan's code works as well, but IMHO my variant is slighly clearer: you don't pass the actual method but the name of the method that should be called.

Ronald Oussoren
Is there an advantage do doing it that way? I like to pass the actual method because a) it feels more "pythonic", with methods being first-class objects, and b) Xcode auto-completes the name of the method for me so I don't have to worry about typos. Also c) I can have a variable hold the end selector if I want.
Dan