Greetings, Monks,

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


In reply to One button text processing by nysus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.