From: Rupert B. <rup...@fr...> - 2005-11-18 23:00:56
|
OK, with much help from Jonathan, I have got the Apple SyncServices "SimpleStickies" [1] translated and working (partially) in Ruby. The SimpleStickies is much more than a simple tutorial, the resulting program is a complete syncing engine, demo-ed on the 'Note' class which implements Simple Stickies. What I did was simply to : 1) modify the Objective-C 'main' to startup with Ruby : this is just the standard Ruby 'main.m' and 'rb_main.rb' 2) replace the 'Note.h' and 'Note.m' files for the Note class by 'Note.rb' and kvc_wrapper.rb [2] By the way, do not forget to create the following directory : ~/Library/Application Support/SyncExamples where the tutorial stores its persistence cache. What is working : - the program runs and starts, - it will save data to a persistence XML file store (in ~/Library/ Application Support/SyncExamples) - it will sync with Tiger's SyncServices (both read and write) - .Mac syncing works natively once the data has got into the SyncServices engine : you can see the SimpleStickies logo in the .Mac prefderences : exiting to thing that this is carying data generated by Ruby ! What I cannot get to work : - reading from the XML persistence file store : this must due to some issue with archiving/unarchiving. To get the program to work properly, delete file in ~/Library/Application Support/SyncExamples before every launch - I cannot programmatically (from Ruby) add objects to the Notes controller to get them inserted into GUI Any help would be welcome to help resolve these two points. Rupert [1] SimpleStickies tutorial : http://developer.apple.com/ documentation/AppleApplications/Conceptual/SyncServicesTutorial/ index.html [2] Here are the files : Notes.rb : # # Note.rb # SimpleStickies adapted to Ruby # # Created by Rupert Barrow on 06/11/05. # Copyright (c) 2005 La Romane. All rights reserved. # require 'osx/cocoa' require 'osx/coredata' require 'kvc_wrapper' class Note < OSX::NSObject include OSX kvc_wrapper_representation :createDate, :originX, :originY, :width, :hei ght, :color kvc_wrapper_representation :text, :translucent, :floating, :shaded, :pri maryKey ns_overrides 'init', 'dealloc' @_representation = nil @_changed = nil def init self.super_init @_representation = NSMutableDictionary.dictionary self.setValue_forKey(NSDate.date, "createDate") self.setValue_forKey(100, "originX") self.setValue_forKey(100, "originY") self.setValue_forKey(400, "width") self.setValue_forKey(300, "height") self.setValue_forKey(NSColor.yellowColor, "color") attrString = NSAttributedString.alloc.initWithString("My simple sticky.") self.setValue_forKey(NSArchiver.archivedDataWithRootObject (attrString), "text") self end def dealloc super_dealloc end def changed @_changed end self.addRubyMethod_withType("changed".to_sym, "v@:@") def setChanged(v) @_changed = v end self.addRubyMethod_withType("setChanged:".to_sym, "v@:@") def representation @_representation end def description "representation = #{@_representation.description}" end def valueForUndefinedKey(k) @_representation.valueForKey(k) end def setValue_ForUndefinedKey(v, k) @_representation.setValueForKey(v, k) end def initWithCoder(coder) self.super_init if (coder.allowsKeyedCoding?) @_representation = coder.decodeObjectForKey("representation") else @_representation = coder.decodeObject; end return self end def encodeWithCoder(coder) if (coder.allowsKeyedCoding?) coder.encodeObject_forKey(@_representation, "representation") else coder.encodeObject(@_representation); end return end end kvc_wrapper.rb : # # kvc_wrapper.rb # SimpleStickies adapted to Ruby # # Created by Rupert Barrow on 05/11/05. # Copyright (c) 2005 La Romane. All rights reserved. # # add kvc_wrapper module OSX module NSKVCBehaviorAttachment def kvc_wrapper_representation(*keys) keys.each do |key| set_key = "set#{key}" set_key[3..3] = set_key[3..3].upcase class_eval <<- EOE_KVC_WRAPPER_REPRESENTATION,__FILE__,__LINE__+1 def #{key} # self.willAccessValueForKey("#{key}") @_representation.valueForKey("#{key}") # self.didAccessValueForKey("#{key}") end def #{key}=(val) self.willChangeValueForKey("#{key}") @_representation.setValue_forKey(val, "#{key}") self.didChangeValueForKey("#{key}") end def #{set_key}(val); self.#{key}= val; end EOE_KVC_WRAPPER_REPRESENTATION self.addRubyMethod_withType("#{key}".to_sym, "v@:@") self.addRubyMethod_withType("#{set_key}:".to_sym, "v@:@") end end end end |