# Copyright (C) 2004, Takaaki Tateishi # ruby wspellcheck.rb [grammar|spell] # require 'win32ole' class WordApplication def initialize(&block) @app = WIN32OLE.new('Word.Application') if( block ) begin yield(self) ensure #close() end end end def create_document() @app.Documents.Add() end def select_all() @app.ActiveDocument.Content.Select() end def selected_text() @app.Selection.Text end def replace(str) @app.Selection.Text = str end def check_spell() @app.ActiveDocument.CheckSpelling() end def check_consistency() @app.ActiveDocument.CheckConsistency() end def check_grammar() @app.ActiveDocument.CheckGrammar() end def close() @app.quit(0) end end $check_type = ARGV[0] $input_file = ARGV[1] $output_file = ARGV[2] WordApplication.new(){|word| word.create_document() word.select_all() File.open($input_file){|f| word.replace(f.read()) } case $check_type when "grammar", "GRAMMAR" word.check_grammar() when "spell", "SPELL" word.check_spell() else $stderr.puts("unknown type: #{$check_type}") exit(1) end text = word.selected_text().chomp() if( $output_file ) File.open($output_file, "w"){|f| text.gsub!("\r","\n") f.puts(text) } else $stdout.puts(text) end }