Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created April 28, 2011 00:58
Show Gist options
  • Save mattetti/945587 to your computer and use it in GitHub Desktop.
Save mattetti/945587 to your computer and use it in GitHub Desktop.
MacRuby sample showing that ABPeoplePickerView is buggy and that it even affects AB.
raise "MacRuby only code, please install http://www.macruby.org/files/nightlies/macruby_nightly-latest.pkg" unless RUBY_ENGINE == 'macruby'
framework 'Cocoa'
framework 'AddressBook'
# AB wrapper code
class ABAddressBook
def <<(record)
if record.is_a?(ABRecord)
"puts adding #{record.name}"
self.addRecord(record)
save
puts "saved"
else
raise TypeError, "You can only add contacts or groups that inherits from ABRecord"
end
end
def delete(record)
if record.is_a?(ABRecord)
self.removeRecord(record)
self.save
else
raise TypeError, "You can only remove contacts or groups that inherits from ABRecord"
end
end
end
# AB wrapper code
class ABGroup
def name=(value)
self.setValue(value, forProperty: 'name')
end
def <<(person)
raise TypeError, "You can only add an ABPerson instance to a group" unless person.is_a?(ABPerson)
self.addMember(person) unless person.parentGroups.map{|name| name}.include?(self.name)
end
end
class AppDelegate
def applicationDidFinishLaunching(notification)
puts "done loading"
end
def applicationWillFinishLaunching(notification)
puts "launching almost done, drawing the window, picker & button"
# setup the window
win = NSWindow.alloc.initWithContentRect([100, 300, 600, 300],
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
backing:NSBackingStoreBuffered,
defer:false)
win.title = 'Bug report'
win.level = 3
win.delegate = self
# add the picker
picker = ABPeoplePickerView.alloc.initWithFrame([0,100, 600, 200])
win.contentView.addSubview(picker)
# add a button to create a group
button = NSButton.alloc.initWithFrame([200, 10, 200, 80])
win.contentView.addSubview(button)
button.bezelStyle = 4
button.title = 'Create group'
button.target = self
button.action = 'create_group:'
win.display
win.orderFrontRegardless
end
def windowWillClose(sender); exit; end
# Create a group called 'TestGroup' and list all the existing groups
def create_group(sender)
address_book = AB
group = ABGroup.alloc.init
group.name = 'TestGroup'
puts "adding test group"
address_book << group
address_book.save
AB.groups.each do |group|
puts "group: #{group.description}"
end
puts "Does the TestGroup appear on your screen, what about in the list above?"
puts "Now run the same experiment but with AB.app open."
end
end
# Clean up before running the app
AB = ABAddressBook.sharedAddressBook
test_group = AB.groups.find{|g| g.name == 'TestGroup'}
if test_group
puts "test group present, deleting it now (#{test_group.inspect})"
AB.delete(test_group)
end
app = NSApplication.sharedApplication
app.delegate = AppDelegate.new
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment