Skip to content Skip to sidebar Skip to footer

Problem With Openpaneldidend In Pyobjc In 10.6

The following code which worked fine under OS X 10.5 now fails on 10.6: @IBAction def addButton_(self, sender): panel = NSOpenPanel.openPanel() panel.setCanChooseDirect

Solution 1:

beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo: has been deprecated in 10.6: http://developer.apple.com/library/mac/#documentation/cocoa/reference/ApplicationKit/Classes/NSOpenPanel_Class/DeprecationAppendix/AppendixADeprecatedAPI.html

struggling on the same problem cause PyObjC has no block signature http://pyobjc.sourceforge.net/documentation/pyobjc-core/blocks.html for beginSheetModalForWindow:completionHandler: and you can only use runModal

my solution:

panel = NSOpenPanel.openPanel()
panel.setCanChooseDirectories_(NO)
panel.setAllowsMultipleSelection_(NO)

panel.setAllowedFileTypes_(self.filetypes)
panel.setDirectoryURL_(os.getcwd())

ret = panel.runModal()
if ret:
    print panel.URL()

panel.URL() returns the user selection.

Solution 2:

As elv notes, beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo: has been deprecated in 10.6, and the new method to use is beginSheetModalForWindow:completionHandler: There's no metadata for this method in the version of PyObjC that shipped with Snow Leopard, but it has since been added, and you can update the appropriate file yourself so that you can use this method. Open /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport and find the element:

<classname='NSSavePanel'>

inside this, add the following:

<methodselector='beginSheetModalForWindow:completionHandler:'><argindex='1'block='true' ><retvaltype='v' /><argtype='i'type64='q' /></arg></method><methodselector='beginWithCompletionHandler:'><argindex='0'block='true' ><retvaltype='v' /><argtype='i'type64='q' /></arg></method>

This is the metadata that the Python side needs in order to get and return the correct types of objects to Objective-C. You can pass any callable for the completion handler, as long as it has the correct signature (i.e., takes an integer argument and returns nothing). An example:

def showOpenPanel_(self, sender):
    openPanel = NSOpenPanel.openPanel()

    def openPanelDidClose_(result):
        if result == NSFileHandlingPanelOKButton:
            openPanel.orderOut_(self)
            image = NSImage.alloc().initWithContentsOfFile_(openPanel.filename())
            self.imgView.setImage_(image)
    openPanel.setAllowedFileTypes_(NSImage.imageFileTypes())
    openPanel.beginSheetModalForWindow_completionHandler_(self.imgView.window(), 
                                                          objc.selector(openPanelDidClose_, argumentTypes='l'))

Post a Comment for "Problem With Openpaneldidend In Pyobjc In 10.6"