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

Hi! Is it possible with Perl (or GTK/Perl) to read from a file and 'paste' the text to any X application.

An example: I read from url.txt and find http://www.perlmonks.org. Can I then at once paste it in netscape or emacs?

Can it be done, or do I have to use another language?

Ciryon

Replies are listed 'Best First'.
Re: 'Paste' data in X
by {NULE} (Hermit) on Oct 03, 2001 at 19:30 UTC
    Hi ciryon,

    Your question is a little ambiguous (could be read several ways) so I'll reply the best I can and if I am off base you can let me know in a reply.

    There are two different ways right off the top I would address your question. The first is handling your exact example. Netscape (Mozilla) definitely provides you an option to supply it an URL on the command line (even if it is already running) and displaying that URL in the browser. There are a number of options that the script used to launch the application provides.

    The more complicated issue (and probably more directly related to your question) is manipulating the clipboard from within a Perl application. All my experience is with Perl/Tk so I cannot directly address Perl/GTK or wxPerl (which I'm dying to learn because I understand is the GUI of choice for Perl these days). From Perl/Tk there is a Tk::clipboard module that provides you direct access to the clipboard through the $widget->clipboardClear and $widget->clipboardAppend methods. This would enable you to load the clipboard (in X and presumably WinXX as well) with pretty much any data you like. From there it would be a manual process to paste that data into your application. That is to say there is method provided by Perl/Tk that allows you force your clipboard into some widget of another application. I also highly doubt that any language would provide for such a method.

    The clipboard isn't generally used as a shared memory area where one application can put data for another to automatically pick that information up. It possibly could be treated that way, but it would be very sensitive to user interruption. KDE has a nice applet that tries to intelligently handle information you put in the clipboard and may also provide you with some of the functionality you desire. However this applet performs the action I first discussed - it calls the application with data on the command line.

    In summary I don't think what you are asking for is directly possible, but playing with the clipboard using Perl is. The documentation for Perl/Tk is pretty good so if you have it check it out. wxPerl may provide you with additional options I am not aware of.

    Good luck!
    {NULE}

Re: 'Paste' data in X
by traveler (Parson) on Oct 03, 2001 at 20:02 UTC
    There seem to be two issues here: 1) getting the data out of your perl app, and 2) getting the data into the other application. Gtk-perl lets you access the clipboard via GTK::Editable. You can select data and paste it into the clipboard under program control. This solves number 1. Number 2 is harder: there is no guarantee that a given X program is "friendly" enough to accept clipboard data (although most probably are). Also, you'd need to be able to tell that application, "Get the data in the clipboard now and paste it at the insertion point." I know of no universal way to do that.

    Gtk-Perl does support drag-and-drop so you may be able to talk to another app that knows drag-and-drop, but again, that is not universal.

    I'm not sure what you really want to do, but if your goal is to talk to netscape see this article for information on how to control a netscape session from the command line (or system/backticks in perl).

    You might also want to see this page for a scheme for doing something similar to what you describe. It uses X directly and I don't know of any perl bindings supporting that.

    HTH, --traveler

Re: 'Paste' data in X
by tachyon (Chancellor) on Oct 03, 2001 at 19:48 UTC

    emacs bah - vi forever:

    #!/usr/bin/perl -w use strict; use LWP::Simple; my $infile = '/urls.txt'; my $outfile = '/webpage.htm'; # get first line from ulrs.txt open F, $infile or die "Can't read $infile $!\n"; chomp (my $line = <F>); close F; # get webbpage my $page = get($line); # write contents to a file open F, ">$outfile" or die "Can't write $outfile $!\n"; print F $page; close F; `vi $outfile`;
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print