Re: [Gtk-osx-users] how does "NSApplicationOpenFile" work?



Hi Olivier, 

You may find my notes in a previous thread on this list helpful: 
Re: [Gtk-osx-users] Notes on handling Apple events with gtk_osxapplication and PyGTK

and also
[Gtk-osx-users] Notes on native OS X save/open dialogs in PyGTK using PyObjC
if you are going down that path.

Here's my code for connecting to NSApplicationOpenFile:

    # ige-mac-integration: render menubar as a normal apple menubar
    if sys.platform == 'darwin':

        import gtk_osxapplication
        macapp = gtk_osxapplication.OSXApplication()

        # Note that on at least gtk 2.18.2, AppleEvents, of which
        # OpenFile is one, are processed on the GTK event queue.
        # Consequently, we will lose OpenFile events if we call
        # utils.flush_gtk_events() before connecting this callback to
        # the "NSApplicationOpenFile" signal.
        def handle_OSX_OpenFile_event(macapp, filename):
            # Note! A limitation of the current GTK-OSX code
            # (2.18) is that we cannot perform any operations
            # involving the GTK run-loop within this handler,
            # therefore we schedule the load to occur afterwards.
            # See gdk/quartz/gdkeventloop-quartz.c in the GTK+ source. 
            
            def callback():
                ui.load_handler(world_model, main_window, filename)
            gobject.idle_add(callback) 

            # Significant! We must indicate we handled this or crash
            # when the NSApp fallsback to open it via our NSDocument,
            # which is just a front to support native open/save dialogs.
            return True 

        if constants.osxbundle_environment():
            # When run via python outside of a bundle, we receive a request to open our toplevel sourcecode! 
            macapp.connect("NSApplicationOpenFile", handle_OSX_OpenFile_event)


def utils.flush_gtk_events(): 
    while gtk.events_pending():
        gtk.main_iteration()

def constants.osxbundle_environment(_cache=[]):
    if not _cache:
        result = False 

        if sys.platform == 'darwin': 
            import gtk_osxapplication
            bid = gtk_osxapplication.quartz_application_get_bundle_id() 
	    # the later occurs when running outside a bundle 
            # under the system python framework. 
 	    result = not (bid is None or bid == 'org.python.pythonapp')

        _cache.append(result) 

    return _cache[0] 

warm regards, 
Richard. 

On 12/02/2011, at 1:22 AM, Olivier Sessink wrote:

> Hi all,
> 
> I'm playing with the "NSApplicationOpenFile" signal. However, if I try
> to open a file form the finder right now a new process is started by
> the finder, but the signal is not trigerred. Is there a piece of
> example code somewhere that shows how this signal works?
> 
> Olivier
> 
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb
> _______________________________________________
> Gtk-osx-users mailing list
> Gtk-osx-users lists sourceforge net
> https://lists.sourceforge.net/lists/listinfo/gtk-osx-users





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]