Here's a short but powerful Tk program that runs on Win32 systems but could easily be exported to any OS, I'm sure.
The program takes any text copied to the clipboard and instantly reformats it for you. For example, you highlight some text in any application than hit CTRL-C. Then you immediately hit CTRL-V and ouila, the text is processed any way you want. This program will allow you to instantly strip out HTML tags, remove comments from code, and eliminate just about any repetitive text processing task that you can think of.
Currently, the program is set up to instantly change the case of text sent to the clipboard. Easily switch between lowercase, uppercase, mixed case, and sentence case.
Special thanks to bobn for informing me about the repeat method that made this code possible. However, if anyone has a more elegant solution, please let me know. Also, please let me know if you see any glaring errors or suggestions for improvement. Here's the stuff:
use strict; use Win32::Clipboard; use Win32::API; use Tk; # Windows constants my ($OnTop, $NoTop, $Top) = (-1, -2, 0); my ($SWP_NOMOVE, $SWP_NOSIZE) = (2, 1); # 'always on top' state my $isOnTop = $NoTop; # Create a Win32::API object for SetWindowPos my $SetWindowPos = new Win32::API("user32", "SetWindowPos", ['N','N',' +N','N','N','N','N'], 'N'); # and one for FindWindow my $FindWindow = new Win32::API("user32", "FindWindow", ['P','P'], 'N' +); my $mode = 0; my $old_contents; my $clip = Win32::Clipboard(); my $button_pressed; my $toggle_mode = 0; my $main = new MainWindow; my $toggle_button = $main->Checkbutton(-text => "Turn on toggle mode", -onvalue => 1, -offvalue => 0, -variable => \$toggle_mode, -command => sub { !($toggle_mode) } ) ->pack(-side => "bottom", expand => "yes", -fill => "x"); my $aotCBut = $main->Checkbutton(-text => "Always on top", -onvalue => $OnTop, -offvalue => $NoTop, -variable => \$isOnTop, -command => sub { # get a handle to the toplevel window containing our Perl/Tk a +pp my $class = "TkTopLevel"; my $name = $main->title; my $NULL = 0; my $topHwnd = $FindWindow->Call($class, $name); if ($topHwnd != $NULL) { # change 'always on top' state $SetWindowPos->Call($topHwnd, $isOnTop, 0, 0, 0, 0, $SWP_N +OMOVE | $SWP_NOSIZE); }; }) ->pack(-side => "bottom", expand => "yes", -fill => "x"); $main->Label(-text => 'Clipboard Case Changer' )->pack; $main->Button(-text => 'To Uppercase', -command => sub { &change_mode(1) } )->pack; $main->Button(-text => 'To Lowercase', -command => sub { &change_mode(2) } )->pack; $main->Button(-text => 'To Sentence Case', -command => sub { &change_mode(3) } )->pack; $main->Button(-text => 'To Mixed Case', -command => sub { &change_mode(4) } )->pack; $main->Button(-text => 'Off', -command => sub { &change_mode(0) } )->pack; $main->repeat(50 => \&run); MainLoop; sub change_mode { print @_; $button_pressed = 1; $mode = shift; } sub run { my $new_contents = $clip->Get(); if ((($new_contents ne $old_contents) && $mode) || $button_pressed +) { if ($mode == 1) { $new_contents = uc($new_contents); } elsif ($mode == 2) { $new_contents = lc($new_contents); } elsif ($mode == 3) { $new_contents =~ tr/A-Z/a-z/; $new_contents =~ s/(\.\s+\b|^\s*\b)(.)/$1 . uc($2)/xge; } elsif ($mode == 4) { $new_contents =~ tr/A-Z/a-z/; $new_contents =~ s/\b(.)/uc($1)/ge; } $clip->Set($new_contents); $old_contents = $new_contents; $button_pressed = 0; $mode = 0 if !($toggle_mode); } }
Update: Code has been modified to allow it to remain "Always on Top" of the other windows. Many thanks to kevin_i_orourke who figured out how to do it many moons ago and posted his results on PerlMonks.
Update to update: OK, last modification, I swear. I need to get to sleep anyway. This is a simple modification to the code but rather hard to explain. I created a (badly named) "toggle mode." When in toggle mode, the last kind of case change you selected stays in effect until you either click "off" or deselect the "toggle mode" check box. In other words, when in toggle mode, all text copied to the clipboard is automatically reformatted. When not in toggle mode, the user must click one of the buttons before text will be reformatted.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff";
$nysus = $PM . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: One button text processing
by tilly (Archbishop) on Jul 15, 2003 at 06:44 UTC | |
by nysus (Parson) on Jul 15, 2003 at 07:24 UTC | |
by bm (Hermit) on Jul 15, 2003 at 11:02 UTC | |
by nysus (Parson) on Jul 15, 2003 at 12:15 UTC | |
by bm (Hermit) on Jul 15, 2003 at 13:03 UTC | |
| |
by tilly (Archbishop) on Jul 16, 2003 at 21:44 UTC | |
by chanio (Priest) on Jul 16, 2003 at 05:24 UTC |