http://www.perlmonks.org?node_id=106651


in reply to Clipboard to file

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: $!"; }

Replies are listed 'Best First'.
Re: Re: Clipboard to file
by Anonymous Monk on Aug 21, 2001 at 21:54 UTC
    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 ?
        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.