From jvansanten at gmail.com Sat Jan 3 20:57:51 2009 From: jvansanten at gmail.com (Jakob van Santen) Date: Sun, 4 Jan 2009 02:57:51 +0100 Subject: [Rubyosa-discuss] Get elements by name Message-ID: Hi, while it seems that RubyOSA is dead at the moment, I thought some people might find this useful: RubyOSA allows you to access elements in a container by index, but not by name (i.e. the equivalent of "set the value of field 'Author' to 'foo' in AppleScript). As it turns out, this can be done entirely on the Ruby side by overriding OSA::ObjectSpecifierList::[](idx) and building a name-based object specifier when idx is a String: class OSA::ObjectSpecifierList def [](idx) if idx.kind_of?(String) then o = obj_spec_with_name(OSA::Element.__new__('utf8', idx)) else idx += 1 # AE starts counting at 1. o = obj_spec_with_key(OSA::Element.__new__('long', [idx].pack('l'))) end o.instance_variable_set(:@app, @app) o.extend OSA::ObjectSpecifier end private def obj_spec_with_name(element) @desired_class.__new_object_specifier__(@desired_class::CODE, @container, 'name', element) end end Now I can interact with BibDesk ( doc.fields['Author'].value=foo )without fighting with AppleScript. Hooray! Cheers, Jakob