PyGTK: Copy a file to clipboard (paste it in Nautilus)

PyGTK provide clipboard functionality for standard targets: text, file(s) and image. Nautilus if does not use the standard URI target for copy/cat/paste file because this target does not provide the desired action (copy or cut).


For copy/cat/paste Nautilus use a special clipboard target named “x-special/gnome-copied-files”. The content of this target is a text with items separated by new line character (“\n”). The first line is the action (“copy” or “cut”) and then one URI per line.

To copy the file /path/abc.txt the target “x-special/gnome-copied-files” contains the text:

copy
file:///path/abc.txt

The class “gtk.Clipboard” has the method

def set_with_data(targets, get_func, clear_func, user_data)

to set the content of the clipboard to any targets and to provide functions for clipboard requests (get/clean).

Using this method I write a simple code to handle copy path to clipboard (gnomeclipboardtools.py):

[include file=wp-content/uploads/2008/12/gnomeclipboardtools.py]

Using it is very simple:

import gnomeclipboardtools
gnomeclipboardtools.clipboard_copy_path( '/path/abc.txt' )

Tags: , , , , ,

One Response to “PyGTK: Copy a file to clipboard (paste it in Nautilus)”

  1. notch-1 Says:

    not working on ubuntu jaunty: in the nautilus status bar i get “no file in the clipboard” or similar…

Leave a Reply