1# Copyright (c) 2006-2008, The RubyCocoa Project.
2# Copyright (c) 2001-2006, FUJIMOTO Hisakuni.
3# All Rights Reserved.
4#
5# RubyCocoa is free software, covered under either the Ruby's license or the 
6# LGPL. See the COPYRIGHT file for more information.
7
8module OSX
9
10  # for NSApplication
11  class NSApplication 
12    
13    class RBCCTemporaryDelegate < OSX::NSObject
14      attr_writer :proc, :terminate
15
16      def applicationDidFinishLaunching(sender)
17	begin
18	  @proc.call
19	rescue Exception => err
20	  warn "#{err.message} (#{err.class})\n"
21	  warn err.backtrace.join("\n    ")
22	ensure
23	  OSX::NSApplication.sharedApplication.terminate(self) if @terminate
24	end
25      end
26
27    end
28
29    def NSApplication.run_with_temp_app(terminate = true, &proc)
30      # prepare delegate
31      delegate = RBCCTemporaryDelegate.alloc.init
32      delegate.proc = proc
33      delegate.terminate = terminate
34      # run a new app
35      app = NSApplication.sharedApplication
36      app.setDelegate(delegate)
37      app.run
38    end
39
40  end
41
42end
43