views:

108

answers:

1

The following code

import gtk
import nautilus
import os
def alert(message):
    """A function to debug"""
    dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, message)
    dialog.run()
    dialog.destroy()

class TestExtension(nautilus.MenuProvider):
    def __init__(self):
        pass

    def get_file_items(self, window, files):
        items = []
        """Called when the user selects a file in Nautilus."""
        item = nautilus.MenuItem("NautilusPython::test_item", "Test", "Test")
        item.connect("activate", self.menu_activate_cb, files)
        items.append(item)
        return items

    def menu_activate_cb(self, menu, files):
        """Called when the user selects the menu."""
        for name in files:
            alert(name)

does not produce any Messages.
However

import easygui
import nautilus
import os

def alert(message):
    """A function to debug"""
    easygui.msgbox(message)

class TestExtension(nautilus.MenuProvider):
    def __init__(self):
        pass

    def get_file_items(self, window, files):
        items = []
        """Called when the user selects a file in Nautilus."""
        item = nautilus.MenuItem("NautilusPython::test_item", "Test", "Test")
        item.connect("activate", self.menu_activate_cb, files)
        items.append(item)
        return items

    def menu_activate_cb(self, menu, files):
        """Called when the user selects the menu."""
        for name in files:
            alert(name)

works nicely and produces the requested Messages.

Can someone explain this - or better - provide a workaround?

(Even better is that when moving the alert()-call into get_file_items() the message appears correctly)

+1  A: 

On the nautilus mailing list Ahmad Sherif found my error:

gtk.MessageDialog is not working with your code because the fifth argument has to be either string or None, and the variable name is of type nautilus.FileInfo, which means you need to call alert(name.get_name()) not just alert(name)
Please refer to the docs for more info about nautilus.FileInfo.

Thanks to Ahmad for pointing this out.

In a second posting Ahmad explained how to debug better:

I think you could've obtained such debug info if you launched Nautilus from terminal. […]
You should quit Nautilus first (nautilus -q) then launch it (nautilus --no-desktop). If [nautilus] did [automatically] re-spawn [after quitting], you should edit /usr/share/applications/nautilus.desktop as follows (A backup of it would be a good idea):

  • Replace "X-GNOME-AutoRestart=true" with "X-GNOME-AutoRestart=false"
  • Comment this line out "X-GNOME-Autostart-Phase=Desktop" by "#" at the beginning of the line. Actually I forgot why I did this but this how my is my configuration :).
  • Add this line "AutostartCondition=GNOME /apps/nautilus/preferences/show_desktop"
  • Finally, you should restart your session, then try quitting and launching again.

Credits go to wrc1944 […] for the respawning solution.

Applying the steps Ahmad explained I was able to see the error-message my faulty code generated.

Nils