Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

chromatic (983)

  (email not shown publicly)

Blog Information [] Profile for chr0matic []

Journal of chromatic (983)

Friday September 03, 2004
07:05 PM

The Command-Line is My File Chooser

[ #20721 ]

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.


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.


# create an absolute path

# check if Mozilla is already running
/usr/bin/mozilla -remote 'ping()'

# launch a new tab, if so
if [ "$STATUS" == 0 ]; then
    exec /usr/bin/mozilla -remote "openurl(file://$FILE,new-tab)"

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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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
    tibook:~ phillip$ ls
    Desktop         Movies          Public          buddyretriever  mysql.dump
    Documents       Mus

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

  • 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 w

  • 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.
  • ... wil handle those file://../../etc URIs for you.
  • 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!