Subject: [sup-talk] missing run-mailcap

From: [email-masked] (John Bent)
Date: Tue, 22 Jan 2008 09:40:05 -0700

I saw some earlier discussion in the list archives about a missing run-mailcap
script.  I had this same problem too on my mac (10.4.11 Power PC).  It's sort
of a kluge but I dealt with this by writing my own /usr/bin/run-mailcap which
just does a simple hash lookup of mime type to rename the file with an
appropriate extension and then uses the mac 'open' command.  I haven't fully
populated the hash table yet; I'll do that lazily.  If the 'file' command
could return an extension, I'd be tempted to use it and avoid the hash table
but the best it can do is return the mime-type which we already know.  Or if
there was some way to pass the mime-type to the 'open' command, that'd be
better too.  But at least this works...

===========================================================================

#! /usr/bin/perl

my %mimes = (
    'image/jpeg'    => 'jpg',
    'text/html'     => 'html',
);

my ( $type, $file ) = split( /:/, $ARGV[1] );
Log( "$type - $file\n" );
my $extension = $mimes{$type};
if ( ! defined $extension ) {
    Log( "Unknown type $type\n" );
} else {
    rename( $file, "$file.$extension" );
    Log( "open $file.$extension\n" );
    system( "open $file.$extension" );
}

#######################################################################

my $log_fd;
sub
Log {
    if ( ! defined $log_fd ) {
        open LOG, ">>/tmp/mailcap.log" or warn "open log: $!\n";
        $log_fd = \*LOG;
    }
    #print "@_";
    print LOG "@_";
}

---

From: [email-masked] (John Bent)
Date: Tue, 22 Jan 2008 15:33:22 -0700

> better too.  But at least this works...

Here's a cleaner version that doesn't have anything hard-coded but relies
on the 'find' command and the MIME::Types perl module:

============================================================================

#! /opt/local/bin/perl

use MIME::Types;

my ( $type, $file ) = split( /:/, $ARGV[1] );
if ( $type eq 'application/octet-stream' ) {
    $type = findType( $file );
}
my $extension = findExtension( $type );

my $newfile    = "$file.$extension";
my $command    = "open $newfile";
rename( $file, $newfile );
Log( "Extension for $type is $extension -> $command\n" );
system( $command ); 

sub
findExtension {
    my $mimetypes           = MIME::Types->new;
    my MIME::Type $mimetype = $mimetypes->type($type);
    my @extensions          = $mimetype->extensions;
    return $extensions[0];
}

sub
findType {
    my $file = shift;
    my $type = `file -i -b $file`;
    chomp( $type );
    return $type;
}

my $log_fd;
sub
Log {

---

From: [email-masked] (John Bent)
Date: Thu, 24 Jan 2008 14:40:08 -0700

Excerpts from John Bent's message of Tue Jan 22 15:33:22 -0700 2008:
> > appropriate extension and then uses the mac 'open' command.  

When sup opens an attachment, it copies the attachment to 
/tmp/sup-attachment-ID and then calls run-mailcap.  But sup knows what name
the file was attached as.  Could sup save the attachment to /tmp/filename?
Or at least append the extension?  Although my preference would be to use
the fullname which is presumably what the file is named on the originating
machine.  Often I'll open an attachment first and if I like it, then I'll save
it using whichever viewer it's in (e.g. someone sends me an MS Word, I open it
in word, and then save it through word).  I then get a dialog about how to
save it which has as a starting point the current path which is 
/tmp/sup-attachment-ID which I'll always have to change.  But if it was the
original attached name, I'd probably often be happy and would prefer 
to keep that name.  

By the way, sup works so well that I've now imported my old school emails
which I'd previously given up on.  I now have blazing fast search over my
last ten years of email!

---

From: [email-masked] (William Morgan)
Date: Thu, 24 Jan 2008 15:26:20 -0800

Reformatted excerpts from John Bent's message of 2008-01-24:
> /tmp/sup-attachment-ID and then calls run-mailcap.  But sup knows what
> name the file was attached as.  Could sup save the attachment to
> /tmp/filename?  Or at least append the extension?

In 0.4 you should see the extension being preserved. Let me know if
that's not the case.

So you should be able to simply call "open" on that file, rather than
having to use your Perl script.

> Although my preference would be to use the fullname which is
> presumably what the file is named on the originating machine. 

This might be doable. I've been using the tempfile Ruby library to
handle these files, which does two things: it obfuscates the name to
avoid collisions, and it ensures the file is deleted once all references
to the variable are garbage-collected. It might not be hard to replace
this with something that only does the latter behavior. I might be able
to programmatically delete the files too, instead of waiting for garbage
collection.

> By the way, sup works so well that I've now imported my old school
> emails which I'd previously given up on.  I now have blazing fast
> search over my last ten years of email!

Excellent!

-- 
William <wmorgan-sup at masanjin.net>

---

From: [email-masked] (John Bent)
Date: Thu, 24 Jan 2008 16:52:36 -0700

> having to use your Perl script.
Yep, just like you say.  Although, I think I do still need to have something
in /usr/bin/run-mailcap that parses 
"--action=view application/vnd.ms-powerpoint:/tmp/foo.ppt," and then execs
"open /tmp/foo.ppt."  But that's much better than having to guess an extension
from the mime-type.

John

---

From: [email-masked] (William Morgan)
Date: Thu, 24 Jan 2008 16:17:17 -0800

Reformatted excerpts from John Bent's message of 2008-01-24:
> Yep, just like you say.  Although, I think I do still need to have
> something in /usr/bin/run-mailcap that parses "--action=view
> application/vnd.ms-powerpoint:/tmp/foo.ppt," and then execs "open
> /tmp/foo.ppt."  But that's much better than having to guess an
> extension from the mime-type.

You don't even need to fake run-mailcap. You should just be able to have
a one-line ~/.sup/hooks/mime-view.rb that looks like:

  system "open #{filename}"

If open is a standard OS X command, then I will add some code to
auto-detect it at some point.

---

From: [email-masked] (Grant Hollingworth)
Date: Thu, 24 Jan 2008 19:57:50 -0500

> auto-detect it at some point.

It is. Unfortunately, you can tell it which application to use, but
not which MIME type.

---

From: [email-masked] (John Bent)
Date: Thu, 24 Jan 2008 21:40:08 -0700

> It is. Unfortunately, you can tell it which application to use, but

True, but if it has the correct extension (which it does in 0.4), you 
don't need to tell it anything.  

That's great that I don't need to fake run-mailcap since there's a hook.

---

From: [email-masked] (John Bent)
Date: Fri, 25 Jan 2008 17:35:17 -0700

> That's great that I don't need to fake run-mailcap since there's a hook.
The hook works (system "open \'#{filename}\'"), but sup thinks it is failing.
I get a message saying view failed, displaying as text.  Maybe it should
be (! system "open \'#{filename}\'") ?  Also, I added the \' because I noticed
before that sometimes filename has spaces in it and then open won't work.

John

---

From: [email-masked] (William Morgan)
Date: Wed, 30 Jan 2008 09:29:30 -0800

Reformatted excerpts from John Bent's message of 2008-01-25:
> The hook works (system "open \'#{filename}\'"), but sup thinks it is
> failing.  I get a message saying view failed, displaying as text.
> Maybe it should be (! system "open \'#{filename}\'") ?

Weird. Kernel#system returns true if the command succeeded and false
otherwise, so it should work as is.

You could try:
  system "open '#{filename}'"
  $?.success?

But there's no reason that should work and the original shouldn't. You
don't have a debugging print statement or anything like that as the
final line of the hook, do you?

> Also, I added the \' because I noticed before that sometimes filename
> has spaces in it and then open won't work.

Good point. Backslashes optional though. :)

---

From: [email-masked] (John Bent)
Date: Wed, 30 Jan 2008 10:54:22 -0700

>   system "open '#{filename}'"
same problem.  the attachment is successfully opened but sup thinks it
failed and sup displays it as text.

> final line of the hook, do you?
nope, just the exact two lines you supplied above.

I wonder if the problem is that system isn't correctly understanding
the return value from open?  

tangerine:~>open K.png
tangerine:~>echo $?
0

hmmm, I would think 0 is the expected success value?  How could I reverse
true and false?  I'm curious to try that but 
! $?.success? 
$?.success?true:false 
don't work, they error out and sup defaults to run-mailcap.

John

---

From: [email-masked] (William Morgan)
Date: Wed, 30 Jan 2008 10:26:03 -0800

> the return value from open?  

Try with irb:

  $ irb
  irb(main):001:0> system "ls"
  [...]
  => true
  irb(main):002:0> system "asdfasdfa"
  => false

What happens with open?

> true and false?  I'm curious to try that but 

This one should work.

As a sanity check:
  true

Should always count as a success. Is that true?

---

From: [email-masked] (John Bent)
Date: Wed, 30 Jan 2008 11:35:22 -0700

=> true

> Should always count as a success. Is that true?
same problem.  It opens successfully, but sup thinks it doesn't and
displays it as text.  All I see in the log buffer is:
 hook: read 'mime-view' from /Users/johnbent/.sup/hooks/mime-view.rb

Ah.  But
false

works perfectly!  (not sure that's expected but I'm happy!)

Thanks,

John

---

From: [email-masked] (William Morgan)
Date: Sat, 02 Feb 2008 19:01:25 -0800

> works perfectly!  (not sure that's expected but I'm happy!)

The only explanation is that you are living in a bizarro world where
true is false and false is true.

Seriously. That makes no sense.