Like brian d foy, I have a lot of little command-line tools to make my life easier. Today, I wrote two more.
One of the parts of Mac OS X I actually miss (and there are a few) is its open command that examines a named file and attempts to open it in an appropriate application. That's an easy program to write, if you aim for 80% effectiveness, but it's so convenient that when I found myself wishing for it today, I spent five minutes writing it.
#!/usr/bin/perl
use strict;
use warnings;
use File::MMagic;
my $file = shift or die "Usage: $0 <filename>\n";
my %subtypes = map { $_ => 1 } 'application/x-zip', 'text/plain', 'text/html';
my %apps =
(
'application/star-office' => 'xooffice',
'application/msword' => 'abiword-2.0',
'application/mozilla' => 'moz_tab',
'image/jpeg' => 'eog',
'image/gif' => 'eog',
'text/html' => 'moz_tab',
);
my %exts =
(
'application/star-office', => qr/\.sxw\Z/,
'application/mozilla', => qr/\.html?\Z/i,
);
my $mm = File::MMagic->new();
while (my ($subtype, $regex) = each %exts)
{
$mm->addFileExts( $regex, $subtype );
}
my $type = $mm->checktype_filename( $file );
$type = $mm->checktype_byfilename( $file ) if exists $subtypes{ $type };
die "Unknown type '$type'\n" unless exists $apps{ $type };
fork and exit;
exec $apps{$type}, $file, @ARGV;
It's worth factoring out the file types, applications, and subtypes into data somewhere, but when I find myself needing to maintain a bigger list, I'll do that.
You might notice a program called moz_tab. What does that do? I usually have Mozilla running with several tabs on a different virtual desktop. (Now you begin to see what I missed from a real window manager when I used Mac OS X!) I do want to open HTML files in Mozilla, but I don't want to open them in a new browser window or, worse, with a different profile. moz_tab checks to see if there's an existing window and opens a new tab or a window as appropriate.
#!/bin/sh
# create an absolute path
DIR=`pwd`
FILE="$DIR/$1"
# check if Mozilla is already running
/usr/bin/mozilla -remote 'ping()'
STATUS=$?
# launch a new tab, if so
if [ "$STATUS" == 0 ]; then
exec /usr/bin/mozilla -remote "openurl(file://$FILE,new-tab)"
fi
# or launch a new window
exec /usr/bin/mozilla "file://$FILE"
It doesn't check for an absolute path before absolutifying the path, but if I need that, I'll add another line.
Having both of these programs available has saved me almost a minute today. That doesn't seem like much, but keep in mind that I'd have spent that minute navigating developer-hostile file chooser windows. If I can avoid that by using the developer-friendly command line in a ubiquitous terminal window, my life is much more pleasant.