kimura wataru
kimur****@i*****
Thu Apr 27 23:52:27 JST 2006
Hi, Some functions of AppKit requires an instance of NSApplication. So we write scripts like sample/HelloWorld.rb. (1)prepare a delagete object of NSApplication (2)create a new app with NSApplication.sharedApplication (3)set (1) as delegate of (2) (4)run (2) I wrote a method for ease to write procedures working with NSApplication. How about this? I feel it is inconvenient that a process must be exited after executing passed block, but we can get some benefits from this method. [example] ---- require 'osx/cocoa' OSX::NSApplication.run_with_temp_app { choice = OSX::NSRunAlertPanel("title", "message", "YES", "NO", nil) puts choice # button "YES": 1(NSAlertDefaultReturn), # button "NO": 0(NSAlertAlternateReturn) } ---- [implementation] ---- module OSX def NSApplication.run_with_temp_app(&proc) # prepare delegate delegate = Class.new(OSX::NSObject).alloc.init def delegate.applicationDidFinishLaunching(sender) begin @proc.call ensure if $! $stderr.puts "error: " + $!.to_s $stderr.puts $!.backtrace end OSX::NSApplication.sharedApplication.terminate(self) end end def delegate.proc=(block) @proc = block end delegate.proc = proc # run a new app app = NSApplication.sharedApplication app.setDelegate(delegate) app.run end end ---- -- kimura wataru