[MlMt] Any doc on writing bundles please?

Rob Willett rob.mailmate at robertwillett.com
Mon Oct 19 04:30:10 EDT 2015


Hi,

We’ve put together a small Perl script that takes text off the  
clipboard (specifically text put on the clipboard by doing a cut and 
paste from Excel), converts it into Markdown and puts it back onto the 
clipboard.

This simplifies our workflow as we copy lots of data tables around.

This script is provided free and as-is. It will probably make your cat 
sterile and your hair fall out, so be warned. There is no checking for 
anything else, loads of assumptions are made, thousands of bugs are in 
the code, its poorly written, error checking is for other people and as 
for documentation…

However on the plus side, it seems to handle empty cells correctly, it 
does actually seem to work (for some value of work). We should probably 
get it going with strict Perl variables but its late on Sunday.

We are using it with Keyboard Maestro and it seems to work IF you use it 
on a control key outside of Excel. I strongly suspect Microsoft of 
grabbing the various control keys for their now nefarious purposes which 
stops it working when Excel has focus.

Might help somebody to use this,

Best of luck

Rob

```
#!/usr/bin/perl

my $input_line = "";
my $pid = open(INPUT , "pbpaste |") || die("Can't execute pbpaste\n");

# This is deliberately long winded. This takes care of when we *might*
have multiple lines in
# the clipboard.

while (<INPUT>)
{
      s/\r/\n/g;
      s/\t/\|/g;
      $input_line .= $_;
}

# print "Input = $input_line\n";

my @lines = split(/\n/ , $input_line);
my $column_widths = [];
my $output = [];
my $no_of_lines = $#lines;
my $no_of_fields;

foreach my $line_count (0 .. $no_of_lines)
{
      my @fields = split(/\|/ , $lines[$line_count]);
      $no_of_fields = $#fields;

      foreach my $field_count (0 .. $no_of_fields)
      {
          my $width = length($fields[$field_count]);
          if (!defined($column_widths->[$field_count]) || $width >
$column_widths->[$field_count])
          {
              $column_widths->[$field_count] = $width;
          }

          $output->[$line_count]->[$field_count] = 
$fields[$field_count];
      }
}

$pid = open(OUTPUT , "| pbcopy") || die("Can't write to pbcopy\n");
# print out a header
foreach my $field_count (0 .. $no_of_fields)
{
      my $column_width = $column_widths->[$field_count];
      printf OUTPUT "| %-${column_width}s " ,
$output->[0]->[$field_count];
}

print OUTPUT "|\n";

#  print out the dividing line
foreach my $field_count (0 .. $no_of_fields)
{
      print OUTPUT "| ";
      foreach my $char_count (1 .. $column_widths->[$field_count])
      {
          print OUTPUT "-";
      }
      print OUTPUT " ";
}
print OUTPUT "|\n";

# Print out the body of the table
foreach my $line_count (1 .. $no_of_lines)
{
      foreach my $field_count (0 .. $no_of_fields)
      {
          my $column_width = $column_widths->[$field_count];
          if ($output->[$line_count]->[$field_count])
          {
              printf OUTPUT "| %-${column_width}s " ,
$output->[$line_count]->[$field_count];
          } else
          {
              printf OUTPUT "| %-${column_width}s " , " ";
          }
      }
      print OUTPUT "|\n";
}

close(OUTPUT);

exit(0);
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freron.com/pipermail/mailmate/attachments/20151019/499dc872/attachment.html>


More information about the mailmate mailing list