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

I'm having some trouble manipulating the X windows clipboard using Tk in Perl. According to the docs, the following should set the clipboard to "clip":

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->clipboardClear(); $mw->clipboardAppend('clip');

Unfortunately, this does not set the clipboard. If I have xclipboard(1) open, it tells me: "CLIPBOARD selection conversion failed".

Does anyone know what this means, and/or how I can manipulate the X clipboard from inside Perl? I can change the PRIMARY selection with no problem but I need to be able to set the clipboard.

TIA

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
(bbfu) (neither $widget-SelectionHandle()) Re: Tk::Clipboard and the X Windows Clipboard
by bbfu (Curate) on Oct 29, 2001 at 21:03 UTC

    Oh, and I should mention that I tried it using the $widget->SelectionOption() methods as well:

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->SelectionHandle( -selection => 'CLIPBOARD', sub { return 'clip' } + ); $mw->SelectionOwn( -selection => 'CLIPBOARD' );

    It gives the same error (in xclipboard). :-( Whereas the following works just fine:

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->SelectionHandle( -selection => 'PRIMARY', sub { return 'clip' } ) +; $mw->SelectionOwn( -selection => 'PRIMARY' );

    Update: Hrm. Upon further testing, I find that the 'PRIMARY' selection was not actually working between applications. It owns the selection ok (ie, whatever was selected before, is no longer), and I can insert the selection properly within my own application but it does not show up in other applications.

    Upon further further testing, I find that it all works just fine if I have a MainLoop in my program. Now all I have to do is find a way to get it to execute a single loop and then exit. I thought there was an easy way to do that but I cannot seem to find it. I can always resort to some chicanery involving $mw->after() ...

    Does anyone know a function that will execute a single Perl/Tk event loop, and then return?

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      DoOneEvent() processes one event. See this article. Most Perl/Tk programs have a MainLoop, though, so the GUI can run.

      HTH, --traveler

        Thanks. That article is exactly what I was looking for. Actually, though, for this particular application, I don't want a GUI. I'm creating a command-line utility to manipulate the X windows selection/clipboard. Tk was just the only thing I could see right off that let me do that.

        Thanks for your help. :-)

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.

        Blah. I can't get it to work with DoOneEvent(), $mw->update(), or even $mw->idletasks(). :-(

        The problem, as far as I can tell, is that it is not based on how many events are executed (though they need to be executed, otherwise the callback is never called) but, rather, on how long the program remains active. With the following code, it almost never works with $ARGV[0] = 1, sometimes works with 2, pretty much always works with 3, and is (presumably) guaranteed with 5 and above.

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->withdraw(); $mw->clipboardClear(); $mw->clipboardAppend('clip'); $mw->after($ARGV[0], [$mw, 'destroy']); MainLoop;

        I don't suppose you have any ideas on how I could go about this in a way that doesn't seem so... well, kludgey?

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.

Re: Tk::Clipboard and the X Windows Clipboard
by kwoff (Friar) on Oct 29, 2001 at 21:26 UTC

    WARNING: I've never used Tk from Perl before.

    FWIW, I just experimented a bit with it, and I get the error you're talking about in xclipboard. But then I tried:

    my $mw = MainWindow->new(); $mw->clipboardClear(); $mw->clipboardAppend('clip'); my $selected = $mw->SelectionGet(); MainLoop;

    i.e., I followed it with SelectionGet() and MainLoop, and it printed 'clip' into the xclipboard window.

    .... erm, but now I'm getting an error:

    PRIMARY selection doesn't exist or form "STRING" not defined at ./tk.p +l line 9

    Man, that's really flakey.

      The reason it works for you is because of the MainLoop, not the SelectionGet(). I stumbled on the fact that MainLoop makes it work just as you were posting. :-) Now I just need to apply traveler's DoOneEvent suggestion...

      The other error you are getting is because no other application owns the selection when you try to get it.

      Thanks for your help. :-)

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.