A Simple Extension

Create an empty file with the following code:

Example 1. A Simple Extension

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from gi.repository import GObject, Gtk, Thunarx

class ThunarxMenuProviderPlugin(GObject.GObject, Thunarx.MenuProvider):
    def __init__(self):
        pass
    
    def get_file_menu_items(self, window, files):
        item = Thunarx.MenuItem(name="TMP:TestFileAction", label="Python File Action", tooltip='', icon=Gtk.STOCK_FILE)
        item.connect("activate", self.__do_something, window)

        return [item]

    def get_folder_menu_items(self, window, folder):
        return [Thunarx.MenuItem(name="TMP:TestFolderAction", label="Python Folder Action", tooltip='', icon=Gtk.STOCK_OPEN)]

    def __do_something(self, item, window):
        print("Doing something")

Save this file as TestExtension.py in the $XDG_DATA_DIR/thunarx-python/extensions folder or ~/.local/share/thunarx-python/extensions. You may need to create this folder. To run, open the terminal and type:

1
2
$ killall thunar
$ thunar

Once Thunar starts, right-click on a file and you should see a new menu item, "Python File Action". It is as simple as that!

As mentioned above, in order to get loaded by Thunar, a python extension must import the Thunarx module, create a class derived from a Thunarx *Provider, then create the methods that will be called by Thunar when it requests information from its providers. In this case, when someone right-clicks on a file, Thunar will ask all of its MenuProviders for additional menu items to show the user. When folders or files are clicked, the get_file_menu_items method is called and a list of Thunarx.MenuItem objects is expected.