Text

Jun 1, 2010
@ 1:10 pm
Permalink

dictionary.rb

Macruby only, since we’re accessing Mac OS X’s dictionary services.

For Macruby 0.6, /System/Library/Frameworks/CoreServices.framework/Frameworks/DictionaryServices.framework/Resources/BridgeSupport/DictionaryServices.bridgesupport needs to be edited.

On line 20, change:

<retval type64='{?=qq}' type='{?=ii}'/>

to

<retval type64='{_CFRange=qq}' type='{_CFRange=ii}'/>

Then you’re good to go:

# Figured out by Craig on the macruby-devel mailing list
framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/DictionaryServices.framework'

word = 'History'
word_len = DCSGetTermRangeInString(nil, word, 0);
result = DCSCopyTextDefinition(nil, word, word_len)

puts "Definition for: #{word}"
puts result

# Or in module form...

module Definable
  def definition
    range = DCSGetTermRangeInString(nil, self.to_s, 0);
    DCSCopyTextDefinition(nil, self.to_s, range)
  end
  def define
    puts self.definition
  end
end

syntax highlighted by Code2HTML, v. 0.9.1