public
Created

usage: clipcat [filename] -- despite being called 'cat', it doesn't concatenate multiple

  • Download Gist
clipcat
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/usr/bin/perl -w
 
# clipcat: the misleadingly-named textClipping outputter for Mac OS X.
# you'll need MacPerl and DeRez. I think the latter ships with OS X,
# but at least comes with the dev tools (which by definition you have
# if you installed MacPerl)
# -------------------------
# by David Kendal (http://dpk.org.uk / @dpkendal)
# bug reports & comments to the address on http://dpk.org.uk/contact/
 
use strict;
use utf8;
use MacPerl;
use File::Basename;
 
my $script = basename($0);
 
my $file = $ARGV[0];
my $fileinfo = MacPerl::GetFileInfo($file);
 
$file = escape($file);
 
if (!-e $file) {
print STDERR "$script: no such file $file.\n"; exit 1;
} elsif ($fileinfo ne 'clpt') {
print STDERR "$script: file $file is not a clipping.\n"; exit 1;
}
 
my @resdata = `DeRez -only 'utf8' '$file'`;
shift(@resdata);
pop(@resdata); pop(@resdata);
 
foreach (@resdata) {
my $line = $_;
chomp $line;
$line =~ s{^\t\$"|" +/\*.+\*/$}{}g;
my @blobs = split / /, $line;
foreach (@blobs) {
my $blob = $_;
my $hex1 = substr $blob, 0, 2;
my $hex2 = substr $blob, 2, 3;
print chr(hex($hex1)).chr(hex($hex2));
}
}
 
exit 0;
 
sub escape { # http://cl.ly/3JGN -- should work to escape the filename
my $text = shift; # for handing to DeRez
$text =~ s/\\/\\\\/g;
$text =~ s/'/\\'/g;
return $text;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.