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 "@_";
}
Excerpts from John Bent''s message of Tue Jan 22 09:40:05 -0700 2008:> 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... >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 { if ( ! defined $log_fd ) { open LOG, ">>/tmp/mailcap.log" or warn "open log: $!\n"; $log_fd = \*LOG; } #print "@_"; print LOG "@_"; }
Excerpts from John Bent''s message of Tue Jan 22 15:33:22 -0700 2008:> Excerpts from John Bent''s message of Tue Jan 22 09:40:05 -0700 2008: > > 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.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!
Reformatted excerpts from John Bent''s message of 2008-01-24:> 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?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>
Excerpts from William Morgan''s message of Thu Jan 24 16:26:20 -0700 2008:> Reformatted excerpts from John Bent''s message of 2008-01-24: > > 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? > > 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. >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
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. -- William <wmorgan-sup at masanjin.net>
* William Morgan [Thu Jan 24 19:17:17 -0500 2008]:> If open is a standard OS X command, then I will add some code to > auto-detect it at some point.It is. Unfortunately, you can tell it which application to use, but not which MIME type.
Excerpts from Grant Hollingworth''s message of Thu Jan 24 17:57:50 -0700 2008:> * William Morgan [Thu Jan 24 19:17:17 -0500 2008]: > > If open is a standard OS X command, then I will add some code to > > auto-detect it at some point. > > It is. Unfortunately, you can tell it which application to use, but > not which MIME type.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.
Excerpts from John Bent''s message of Thu Jan 24 21:40:08 -0700 2008:> Excerpts from Grant Hollingworth''s message of Thu Jan 24 17:57:50 -0700 2008: > > * William Morgan [Thu Jan 24 19:17:17 -0500 2008]: > > > If open is a standard OS X command, then I will add some code to > > > auto-detect it at some point. > > > > It is. Unfortunately, you can tell it which application to use, but > > not which MIME type. > > 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. >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
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. :) -- William <wmorgan-sup at masanjin.net>
Excerpts from William Morgan''s message of Wed Jan 30 10:29:30 -0700 2008:> 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? >same problem. the attachment is successfully opened but sup thinks it failed and sup displays it as text.> 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? >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
Reformatted excerpts from John Bent''s message of 2008-01-30:> I wonder if the problem is that system isn''t correctly understanding > 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?> 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?This one should work. As a sanity check: system "open ''#{filename}''" true Should always count as a success. Is that true? -- William <wmorgan-sup at masanjin.net>
Excerpts from William Morgan''s message of Wed Jan 30 11:26:03 -0700 2008:> Reformatted excerpts from John Bent''s message of 2008-01-30: > > I wonder if the problem is that system isn''t correctly understanding > > 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> > 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? > > This one should work. > > As a sanity check: > system "open ''#{filename}''" > 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 system "open ''#{filename}''" false works perfectly! (not sure that''s expected but I''m happy!) Thanks, John
Reformatted excerpts from John Bent''s message of 2008-01-30:> Ah. But > system "open ''#{filename}''" > false > > 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. -- William <wmorgan-sup at masanjin.net>