Sal Scotto
sal.s****@gmail*****
Wed Feb 13 10:56:41 JST 2008
require 'osx/cocoa' class CmdTask attr_accessor :task_obj, :status TASK_DONE = 1<<0 TASK_TERM = 1<<1 TASK_COMP = TASK_DONE|TASK_TERM end class CmdRunner attr_accessor :delegate, :start, :finished, :data def initialize end def run_command(cmd, args) @task = OSX::NSTask.alloc.init @pipe = OSX::NSPipe.alloc.init @pipH =****@pipe***** @task.setLaunchPath(cmd) @task.setStandardError(@pipe) @task.setStandardOutput(@pipe) @task.setArguments(args) center = OSX::NSNotificationCenter.defaultCenter center .addObserver_selector_name_object (self,'readPipe:',OSX::NSFileHandleReadCompletionNotification,nil) center .addObserver_selector_name_object (self,'statusPipe:',OSX::NSTaskDidTerminateNotification,nil) @pipH.readInBackgroundAndNotify @task.launch @delegate.send(@start) @taskd = CmdTask.new @taskd.task_obj = @task @taskd.status = 0 end def readPipe(ntf) inData = ntf.userInfo.objectForKey(OSX::NSFileHandleNotificationDataItem) if inData.length > 0 then ntf.object.readInBackgroundAndNotify str = OSX::NSString.alloc.initWithData_encoding(inData, OSX::NSASCIIStringEncoding) puts "R: #{str}" @delegate.send(@data, str.to_ruby) else taskDone(CmdTask::TASK_DONE) end end def statusPipe(ntf) @exit_status = ntf.object.terminationStatus taskDone(CmdTask::TASK_TERM) end def taskDone(flag) s = (@taskd.status.to_i | flag) @taskd.status = s if****@taskd***** == CmdTask::TASK_COMP cleanup @delegate.send(@finished, at exit_status) end end def terminate if @task && @task.isRunning @task.terminate end end def cleanup center = OSX::NSNotificationCenter.defaultCenter center.removeObserver(self) @taskd.task_obj = nil @taskd = nil @pipe = nil end end no crashes or hangs my app since the 10.5.2 update