#!/usr/bin/env ruby # 2009/05/18 # Snippet to compile a file using nv-metapost.engine or nv-metafun.engine # To use in Smultron, copy and paste into a command snippet # (don't forget to set the interpreter to /usr/bin/ruby). # Try to uncomment the following line if the script does not work correctly #require 'ftools' ENGINE='/Users/XXXXXXXXXX/Library/TeXShop/Engines/nv-metapost.engine' PDFVIEWER='/Applications/TeXShop.app' # Change as needed TEXBIN='/usr/texbin' VERBOSE=false # Set to true for debugging purposes ENV['PATH'] = TEXBIN + ':' + ENV['PATH'] puts 'PATH = ' + ENV['PATH'] if VERBOSE # Preflight paths unless (File.exists?(ENGINE)) puts "Sorry, I cannot find #{ENGINE}." puts "Please set the ENGINE variable correctly." exit(1) end unless (File.exists?(PDFVIEWER)) puts "Sorry, I cannot find #{PDFVIEWER}" puts "Please set the PDFVIEWER variable at the beginning of this code snippet." exit(1) end unless (File.directory?(File.join(TEXBIN))) puts "Sorry, I cannot find the directory #{TEXBIN}." puts "Please set the TEXBIN variable at the beginning of this code snippet." exit(1) end # Ok, we're ready for typesetting jobpath = %%p # For using from within Smultron #jobpath = ARGV[0] # For using from the command line puts "Job path: #{jobpath}" if VERBOSE workingdir = File.dirname(jobpath) suffix = File.extname(jobpath) jobname = File.basename(jobpath, suffix) puts "Working dir: #{workingdir}" if VERBOSE puts "Job name: #{jobname}" if VERBOSE puts "Suffix: #{suffix}" if VERBOSE if ( system("cd '#{workingdir}'; perl '#{ENGINE}' '#{jobname}#{suffix}' 2>&1") ) pdf = File.join(workingdir, jobname + '.pdf') puts "PDF path: #{pdf}" if VERBOSE if (File.exists?(pdf)) unless ( system("open -a '#{PDFVIEWER}' '#{pdf}'") ) puts "An error has occurred on trying to run #{PDFVIEWER} #{pdf}" end else puts "Compilation succeeded, but no pdf has been generated." end else puts "Sorry, an error has occurred during compilation :(" end