open

pemungkah on 2005-01-24T22:35:51

I love OS X's open command, which FreeBSD doesn't have; it allows me to be lazy on an unprecedented scale.

So I found Scott Lawrence's version, which was okay as far as it went, but I wanted to add Firefox support for .html files. Unfortunately Scott's version assumed that cmd args would work for anything, and firefox blah.html doesn't work as expected.

So the obvious right thing to do was fix his version so it would allow me to do arbitrary callbacks to generate the command string to execute. And since I was in there anyway, I decided to clean up the structure a little.

So here's a Perl version of open:

#!/usr/bin/perl
#
# 'open.pl'  v1.0
#
#   Fri Jan  3 14:36:57 EST 2003
#
#    a simple little script that makes migrating from OS X
#    back to solaris a little easier. 
#
#  it checks each argument with the command "file" and then the
#  file extension to see if it knows what to do with it.
#
#  Version 1.1 
#
#   Mon Jan 24 14:00:00 PST 2005
#
#    Extension to allow callbacks to return a command string.
#    Tested on FreeBSD. Cleaned up style.

# the result from running 'file' on the file %filehash = ( 'ascii text' => ["$ENV{PAGER}"], 'JPEG file' => ['xv'], 'JPG file' => ['xv'], 'tiff format image' => ['xv'], 'PBM ascii file' => ['xv'], 'PGM ascii file' => ['xv'], 'PPM ascii file' => ['xv'], 'PBM raw file' => ['xv'], 'PGM raw file' => ['xv'], 'PPM raw file' => ['xv'], 'TIFF file, big-endian' => ['xv'], 'TIFF file, little-endian' => ['xv'], 'GIF file, v87' => ['xv'], 'GIF file, v89' => ['xv'], 'IFF ILBM file' => ['xv'], 'PostScript document' => ['gs'], 'Adobe Portable Document Format (PDF) v1.0' => ['acroread'], 'Adobe Portable Document Format (PDF) v1.1' => ['acroread'], 'Adobe Portable Document Format (PDF) v1.2' => ['acroread'], 'HTML document text' => ['firefox', sub { my ($cmd, $arg) = @_; my $do = "$cmd file://". ($arg =~ m{^/} ? "" : `pwd`."/"). "$arg"; $do =~ s/\n//; $do; } ], );

# for "text" or "data": %exthash = ( 'txt' => [$ENV{PAGER}], 'pl' => [$ENV{PAGER}], 'cgi' => [$ENV{PAGER}], 'mp3' => ["mpg123"], 'pdf' => ["acroread"], 'ps' => ["gs"], );

$arg = ""; while(@ARG $arg = shift;

next if (!-e $arg); next if (-d $arg);

# clean up 'file' type $result = `file $arg`; # name:\ttype\n; $type= (split /:/, $result)[-1]; $type =~ s/^\s+//g; $type =~ s/\s+$//g;

# check the file hash if (defined $filehash{$type}) { call(\%filehash, $type, $arg) or sys(\%filehash, $type, $arg); next; }

# check the extension hash $extension = (split /\./, $arg)[-1]; if ( defined $exthash{$extension} ) { call(\%exthash, $type, $arg) or sys(\%filehash, $type, $arg); next; }

# lose. print "Unknown type: $arg\n"; }

sub call { my ($hash, $key, $arg) = @_; my $callback = $hash->{$key}->[1]; $callback and (system $callback->($hash->{$key}->[0], $arg) or 1); }

sub sys { my ($hash, $type, $arg) = @_; system "$hash->{$type}->[0] $arg"; }



Obviously it could be refactored even further, but this is good enough to work with. An external file holding the extension definitions, or using the system MIME types, is an obvious direction.


deja vu all over again

lachoy on 2005-01-25T01:30:54

chromatic did this a while ago...