The Command-Line is My File Chooser

chromatic on 2004-09-04T00:05:11

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 \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.


Which OSX?

phillup on 2004-09-04T15:20:38

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.

I'm not sure which version you are running, but Panther definitely has an open command.
tibook:~ phillip$ which open
/usr/bin/open
tibook:~ phillip$ ls
Desktop         Movies          Public          buddyretriever  mysql.dump
Documents       Music           Sites           dns.txt
Library         Pictures        benchmarks      mbox            test.pl
tibook:~ phillip$ open dns.txt
And... up pops textedit with the file loaded. (Which reminds me that I haven't associated BBEdit with .txt files.)

This is especially handy when you give it a hidden directory as an argument... It will open the directory in the finder for you.

Re:Which OSX?

chromatic on 2004-09-04T17:23:26

It was a misleading sentence.

I don't use Mac OS X anymore. The open command is one of the few features of Mac OS X that I didn't have under Linux -- and I missed it.

Factoring out the file types and applications

Aristotle on 2004-09-05T01:32:31

There's already a place for them: ~/.mailcap (MIME type to app map) and ~/.mime.types (extension to MIME type map), as well as non-dot versions in /etc. Parsers should exist on CPAN.

I've toyed with the idea you're presenting here many times, but never actually bothered to do something about that itch.

I did write a dirty tool called rat though, which does a subset of what open might, for tarballs (it also gained support for zips and rars). The name is because tar is backwards… :-) It just barely works well enough to be sufficient for 99.9% of cases, which doesn't motivate me to fix it, which means it remains so stupid I don't want to publish it. :-) Ah, programmers…

rox filer

phlaegel on 2004-09-05T07:05:58

I use rox filer this way occasionally. I have rox all set up with app->file mappings, and then just run 'rox somefile.blah' to get the right app.

URI::ImpliedBase

pemungkah on 2004-09-05T15:40:15

... wil handle those file://../../etc URIs for you.

Can also use "gnome-open" or similar...

sharumpe on 2007-08-17T17:27:04

Just in case other folks find this the way I did...

Another work-alike of the Mac OS X "open" command is "gnome-open", which is newer than chromatic's post, I think. I can't find any documentation that looks official, but there is a decent write-up here

Hope this helps!