#!/usr/local/bin/perl -w #The directory this program is running from. use File::Basename; my $pathBase; BEGIN { unshift(@INC, "."); $pathBase = dirname($0); unshift(@INC, $pathBase); } use strict; use Carp qw( cluck ); use Win32::GUI; use Win32::GUI::Loft::Design; use Win32::Clipboard; use Data::Dumper; use Text::Wrap qw(fill); #Number of cols to reformat/flow text. May change. my $colsFlow = 60; my $textNameProgram = "Clipboard Transformer 0.01"; my $winTransformer = undef; my $mnuTray = Win32::GUI::MakeMenu( "tray" => "tray", " > &Text" => "mnuTrayText", " > > &Count" => "mnuTrayCount", " > > > &Words" => "mnuTrayCountWords", " > > &URL" => "mnuTrayUrl", # " > > Fix and &open" => "mnuTrayUrlFixOpen", " > > > &Fix" => "mnuTrayUrlFix", " > > &Format" => "mnuTrayFormat", " > > > Remove formatting" => "mnuTrayFormatRemove", " > > > Flow" => "mnuTrayFormatFlow", # " > > Flow to..." => "mnuTrayFormatFlowTo", " > &Template" => "mnuTrayTemplate", " > > &Fill" => "mnuTrayTemplateFill", " > &RegExp" => "mnuTrayRegexp", " > > Substitute" => "mnuTrayRegexpSubst", " > -" => 0, " > E&xit" => "mnuTrayExit", ); eval { main(); }; Win32::GUI::MessageBox(0, "Error: $@", "Clipboard Transformer") if($@); sub main { #Load Win32::GUI::Loft windows { my $fileWindow = "resource\\regexp_substitute.gld"; my $objDesign = Win32::GUI::Loft::Design->newLoad($fileWindow) or die("Could not open window file ($fileWindow)"); $objDesign->buildWindow() or die("Could not build window ($fileWindow)"); } { my $fileWindow = "resource\\fill_template.gld"; my $objDesign = Win32::GUI::Loft::Design->newLoad($fileWindow) or die("Could not open window file ($fileWindow)"); $objDesign->buildWindow() or die("Could not build window ($fileWindow)"); } #Build fake window to hang the notifyicon on $winTransformer = new Win32::GUI::Window( -left => 13, -top => 32, -width => 439, -height => 260, -name => "winTransformer", -text => "Clipboard Transformer" ); #Tray icon my $fileIcon = "resource/transformer.ico"; my $icoTray = new Win32::GUI::Icon($fileIcon) or die("Could not load icon ($fileIcon)\n"); my $niTray = Win32::GUI::NotifyIcon->new($winTransformer, -name => "niTray", -id => 1, -icon => $icoTray, -tip => $textNameProgram, ); Win32::GUI::Dialog(); return(1); } =head1 EVENT HANDLERS: MAIN =head2 niTray_Click() Popup tray menu. =cut sub ::niTray_Click { return(::niTray_RightClick(@_)); } =head2 niTray_RightClick() Popup tray menu. =cut sub ::niTray_RightClick { defined($winTransformer) or return(1); my ($x, $y) = Win32::GUI::GetCursorPos(); $winTransformer->TrackPopupMenu($mnuTray->{tray}, $x, $y); return(1); } =head2 mnuTrayCountWords_Click() Count words and put the count in the clipboard =cut sub ::mnuTrayCountWords_Click { my $clipboard = Win32::Clipboard::GetText(); my @aWord = split(/\W+/s, $clipboard); Win32::Clipboard::Set( scalar(@aWord) ); return(1); } =head2 mnuTrayUrlFix_Click() Fix the URL by removing newlines etc. =cut sub ::mnuTrayUrlFix_Click { my $clipboard = Win32::Clipboard::GetText(); $clipboard =~ s{\n}{\r\n}gs; #Remove anything before "http/ftp/mailto" $clipboard =~ s{^([\s\S]*?)( (and whitespace) at the beginning of lines and remove newlines $clipboard = join("", map { $_ =~ s{^[>\s]+}{}; $_ } split(/\n/, $clipboard)); #Remove whitespace $clipboard =~ s{\s}{}gs; #Remove enclosing < > $clipboard =~ s{^<([\s\S]+)>$}{$1}; #Remove beginning < (may be left from the http/ftp/mailto match) $clipboard =~ s{^<([\s\S]+)$}{$1}; Win32::Clipboard::Set( $clipboard ); return(1); } =head2 mnuTrayUrlFixOpen_Click() Fix the URL by removing newlines etc. and open it in the default browser. =cut sub ::mnuTrayUrlFixOpen_Click { ::mnuTrayUrlFix_Click(); my $clipboard = Win32::Clipboard::GetText(); $clipboard =~ s{\n}{\r\n}gs; system("start $clipboard"); return(1); } =head2 mnuTrayFormatRemove_Click() Remove any formatting, leaving the actual text contents. =cut sub ::mnuTrayFormatRemove_Click { my $clipboard = Win32::Clipboard::GetText(); Win32::Clipboard::Set( $clipboard ); return(1); } =head2 mnuTrayFormatFlow_Click() Reflow/reformat the text to a certain number of columns. =cut sub ::mnuTrayFormatFlow_Click { my $clipboard = Win32::Clipboard::GetText(); #$clipboard =~ s{\n}{\r\n}gs; $Text::Wrap::columns = $colsFlow; $clipboard = fill("", "", $clipboard); $clipboard =~ s{\x0A}{\r\n}gs; Win32::Clipboard::Set( $clipboard ); return(1); } =head2 mnuTrayTemplateFill_Click() Display Fill Template dialog box. =cut sub ::mnuTrayTemplateFill_Click { defined(my $win = $Win32::GUI::Loft::window{winTemplate}) or return(1); $win->reFTData()->Text( Win32::Clipboard::GetText() ); if(!$win->IsVisible()) { #Position the window at the mouse cursor my ($l, $t) = Win32::GUI::GetCursorPos(); $win->Move( $l - $win->Width(), $t - $win->Height() ); $win->Show(); $win->SetForegroundWindow(); $win->reFTTemplate()->SetFocus(); } else { $win->SetForegroundWindow(); } return(1); } =head2 mnuTrayRegexpSubst_Click() Display regexp dialog box. =cut sub ::mnuTrayRegexpSubst_Click { defined(my $win = $Win32::GUI::Loft::window{winRegexpS}) or return(1); $win->reRESInput()->Text( Win32::Clipboard::GetText() ); if(!$win->IsVisible()) { #Position the window at the mouse cursor my ($l, $t) = Win32::GUI::GetCursorPos(); $win->Move( $l - $win->Width(), $t - $win->Height() ); $win->Show(); $win->SetForegroundWindow(); $win->tfRESExpression()->SetFocus(); } else { $win->SetForegroundWindow(); } return(1); } =head2 mnuTrayExit_Click() Exit program. =cut sub ::mnuTrayExit_Click { return(-1); } =head1 EVENT HANDLERS: FILL TEMPLATE =head2 winTemplate_Terminate() Hide window, don't destroy it. =cut sub ::winTemplate_Terminate { defined(my $win = $Win32::GUI::Loft::window{winTemplate}) or return(1); $win->Hide(); return(0); } =head2 btnFTFill_Click() Perform the substitution. =cut sub ::btnFTFill_Click { defined(my $win = $Win32::GUI::Loft::window{winTemplate}) or return(1); my $clipboard = $win->reFTData()->Text(); print "RAW((($clipboard)))}n"; $clipboard =~ s/\x0D\x0D/\x0D/gs; #$clipboard =~ s/\x0D/\n/gs; print "MIX((($clipboard)))}n"; my $template = $win->reFTTemplate()->Text(); my $sep = "\t"; if($win->rbFTSepChar()->Checked()) { $sep = $win->tfFTSepChar()->Text(); } my @aResult; for my $line (split(/\n/, $clipboard)) { chomp($line); print "((($line)))\n"; my @aCol = split(/$sep/, $line); my $resLine = $template; $resLine =~ s/\$(\d)/$aCol[$1 - 1] || ""/seg; push(@aResult, $resLine); } #Back to clipboard $clipboard = join( #"\n", "\x0D", @aResult); #print "BEFORE((($clipboard)))\n"; # $clipboard =~ s/\x0D\x0A/\x0A/gs; #$clipboard =~ s/\x0D/\n/gs; #print "to 0A((($clipboard)))\n"; # $clipboard =~ s{\x0A}{\r\n}gs; Win32::Clipboard::Set( $clipboard ); # $win->reFTResult()->Text($clipboard); return(0); } =head1 EVENT HANDLERS: REGEXP S =head2 winRegexpS_Terminate() Hide window, don't destroy it. =cut sub ::winRegexpS_Terminate { defined(my $win = $Win32::GUI::Loft::window{winRegexpS}) or return(1); $win->Hide(); return(0); } =head2 btnRESGo_Click() Perform the substitution. =cut sub ::btnRESGo_Click { defined(my $win = $Win32::GUI::Loft::window{winRegexpS}) or return(1); my $clipboard = $win->reRESInput()->Text(); my $regexp = $win->tfRESExpression()->Text(); #Auto-prepend a s $regexp = "s$regexp" if($regexp !~ /^s/); print "regexp ($regexp)\n"; #Try the regexp on nonsense data eval qq { my \$foo = "bar"; \$foo =~ $regexp; print "tested\n"; }; if($@) { Win32::GUI::MessageBox(0, "RegExp error: $@", "Clipboard Transformer"); return(1); } #Line by line? my @aLine = ($clipboard); @aLine = split(/\n/, $clipboard) if($win->chbRESLine()->Checked()); chomp(@aLine); #Substitution my @aLineNew; for my $s (@aLine) { print "before ($s)\n"; eval qq { \$s =~ $regexp; }; print "after ($s)\n\n"; push(@aLineNew, $s); } #Back to clipboard $clipboard = join("\n", @aLineNew); $clipboard =~ s{\x0A}{\r\n}gs; Win32::Clipboard::Set( $clipboard ); # $win->reRESOutput()->Text($clipboard); return(0); } #EOF