#!/usr/bin/env ruby # vim:ft=ruby # # forks and executes the requested program with the temporary file given, # allowing mutt to return to its message view while you view the attachment in # the program # # usage in ~/.mailcap: # application/*; ~/bin/mutt_bgrun %t %s # require "fileutils" require "open3" mime = ARGV[0] orig_file = ARGV[1] if !orig_file STDERR.puts "usage: #{$0} " exit 1 end if mime == "application/octet-stream" if orig_file.to_s.match(/\.pdf$/i) mime = "application/pdf" end end viewer = nil if `uname`.strip == "Darwin" viewer = "open" else case mime.to_s.downcase when "application/pdf", /^image\// viewer = "firefox" end end if !viewer STDERR.puts "no viewer for type #{mime.inspect}" exit 1 end tmp_dir = `mktemp -d`.chomp tmp_file = tmp_dir + "/" + File.basename(orig_file).gsub(/[^A-Za-z_\.-]/, "") launch_file = tmp_file.dup if viewer == "firefox" launch_file = "file:///#{launch_file}" end system("cp", orig_file, tmp_file) Kernel.fork do i, _, _, w = Open3.popen3(viewer, launch_file) i.close begin Process.waitpid(w[:pid]) rescue Errno::ECHILD # process ended really quickly, most likely just handed it off to another # process, so wait a bit for it to process the tmp file before removing it sleep 5 end sleep 5 File.unlink tmp_file Dir.unlink tmp_dir end