Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am writing a Perl/Tk application for Gnome, and would like to use the clipboard to move files between my app and the Gnome file manager. One way is easy: inspecting the clipboard, as set by Gnome.
But setting the clipboard so that Gnome can use it, I have not managed.

Here is my demo program:

#!/opt/perl/bin/perl -w use Tk; use strict; my $mw = 'MainWindow'->new(); my $tb = $mw->Frame->pack( -fill => 'x' ); $tb->Button( -text => "GET CLIPBOARD", -command => \&get_clipboard )->pack( -side => 'left' ); $tb->Button( -text => "SET CLIPBOARD", -command => \&set_clipboard )->pack( -side => 'left' ); MainLoop(); sub get_clipboard { print "\n"; my $own = $mw->SelectionExists( -selection => 'CLIPBOARD' ); if( $own ) { printf "Owner of CLIPBOARD is 0x%x\n",$own; eval { my @targ = $mw->SelectionGet( -selection => 'CLIPBOARD', 'TARGETS' ); for my $targ ( @targ ) { my @t = $mw->SelectionGet( -selection => 'CLIPBOARD', $targ ); local $" = ', '; print "$targ => [ @t ]\n"; } }; my $e = $@; if( $e ) { print $e; } } } sub set_clipboard { print "\n"; $mw->SelectionOwn( -selection => 'CLIPBOARD' ); $mw->SelectionHandle( -selection => 'CLIPBOARD', -type => 'MULTIPLE', -format => 'INTEGER', sub { return ( 325, 323, 324, 462 ) } ); my $file = "/home/philou/Desktop/gtkperl-tutorial.ps"; $mw->SelectionHandle( -selection => 'CLIPBOARD', -type => 'x-special/gnome-copied-files', sub { return "copy\nfile://$file" } ); $mw->SelectionHandle( -selection => 'CLIPBOARD', -type => 'UTF8_STRING', sub { return "file://$file\n" } ); }

get_clipboard works alright, but how to write set_clipboard is a mystery to me. I guess I need to understand what is exactly the 'MULTIPLE' target, and how to set it, and other things too.
Do someone has an idea to do that properly ?

Thanks,

Philippe

Replies are listed 'Best First'.
Re: Copy files to Perl/Tk clipboard
by zentara (Cardinal) on Nov 15, 2006 at 13:21 UTC
    I don't have Gnome setup, but I do remember messing with this awhile back, and found that the Tk Text widget was a reliable way of setting the clipboard. See Tk copy to mouse clipboard It will copy the text to the mouse paste clipboard as well as the Edit menu paste command. It will copy to Gtk2 programs, but the Gnome Desktop? I don't know. Hope it helps. See also Re: Move/Copy files using Clipboard! where you can reliably use the xclip command thru system.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Copy files to Perl/Tk clipboard
by Argel (Prior) on Nov 16, 2006 at 21:28 UTC
    Here is a code snippet from a Perl/Tk password generating app I wrote that copies stuff to the clipboard. Hope it helps.
    my($entry) = $main_frame->Entry ( -textvariable => \$password ); $entry->clipboardClear(); $entry->clipboardAppend($password); $entry->SelectionClear(); $entry->SelectionHandle( sub { $password } ); $entry->SelectionOwn;