Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First, you should be able to turn autoflush on for the filehandle you're using to output (see my example above). That should ensure that whatever you write to the file gets flushed immediately.

Second, yes, you should be able to flush the clipboard with the line: $clipboard->Empty();

Type "perldoc Win32::Clipboard" at your Windows Command prompt to read more about the Clipboard module. I would recommend taking a look at POSIX::strftime for formatting your date/time into a file name. Here's an updated version of the script that uses strftime. It creates an output file with a name like: clip_2001_Aug_21_15_51_11.txt but of course you can customize any way you like.

#!/bin/perl -w use strict; use Win32::Clipboard; use FileHandle; use POSIX; my $clipboard = Win32::Clipboard(); print "To exit, CNTL-C out of this window.\n\n\n"; $clipboard->Empty(); while (1) { $clipboard->WaitForChange(); my $text = $clipboard->GetText(); my $now = localtime(); my $file = POSIX::strftime("c:\\temp\\clip_%Y_%b_%d_%H_%M_%S.txt", +localtime()); open(OUTPUT,">$file") or die "open: $!"; binmode OUTPUT; autoflush OUTPUT 1; print "$now: writing " , substr($text,0,40) , "... to $file\n"; print OUTPUT "$now\n$text\n"; close OUTPUT || die "close: $!"; } exit;
Note, I noticed that on my system (Win2K, ActiveState build 623) that this write a file as soon as it's run even though the clipboard hasn't changed. I'm not sure why that is -- it seems to work fine once it's running. This should get you started though.

In reply to Re: Re: Re: Re: Clipboard to file by RhetTbull
in thread Clipboard to file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found