Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Clipboard to file

by Anonymous Monk
on Aug 21, 2001 at 20:34 UTC ( #106632=perlquestion: print w/replies, xml ) Need Help??

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

I have no knowledge about writing Perl. I have seen the nice script about tidying the text in clipboard which works great. Is there anyone who can write a little script that saves the clipboard to a text file? That is when I mark a text on a webpage and hit copy (Ctrl+C) it will save the marked text to a file? Best regards Rolf

Replies are listed 'Best First'.
Re: Clipboard to file
by clemburg (Curate) on Aug 21, 2001 at 21:06 UTC

    Put this into a ".bat" file, e.g., "clipfile.bat" (one line!):

    @perl -MWin32::Clipboard -e "binmode(*STDOUT); $c=Win32::Clipboard(); +print $c->Get();" > clipfile.txt

    Update: Added binmode() call as in tilly's suggestion.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

      You should binmode STDOUT or else the newlines may be messed up. With that in place:
      @perl -MWin32::Clipboard -e "binmode(*STDOUT); print Win32::Clipboard- +>Get();"
Re: Clipboard to file
by RhetTbull (Curate) on Aug 21, 2001 at 21:18 UTC
    Try this. It will time tag and append each clipboard change to a file.
    #!/bin/perl -w use strict; use Win32::Clipboard; use FileHandle; my $clipboard = Win32::Clipboard(); #open the log file my $file = 'c:\temp\clip.txt'; open(OUTPUT,">>$file") or die "open: $!"; binmode OUTPUT; autoflush OUTPUT 1; print "Clipboard being saved to $file\n"; print "To exit, CNTL-C out of this window.\n\n\n"; while (1) { $clipboard->WaitForChange(); my $text = $clipboard->GetText(); my $now = localtime(); print "$now: writing " , substr($text,0,40) , "... to $file\n"; print OUTPUT "$now\n$text\n"; } exit; END { close OUTPUT or die "close: $!"; }
      You guys are good. I have some trouble with the one that stays resident, which is the one I would like to have working, as there are many clips I need to copy and have as files. So I do not want an append to file, but simply a file named by date+time. The latter I can figure out myself, but the problem is that this code does make a file, but writes nothing in it. 0kb. So if you can get it to work for a fixed file name without append, I would be extremly happy. Sincerely Rolf
        Sorry, It did write to file. It just took some time before it appeared after I closed the script. My fault, I should have understood that. Would it also be possible to flush the clipboard when it first starts, so that anything in the clipboard is first deleted and then it waits for a new CTRL+C ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://106632]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2021-04-20 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?