Module: OSA

Defined in:
lib/rubyosa/rbosa_properties.rb,
ext/rubyosa/rbosa.c,
lib/rubyosa/rbosa.rb

Defined Under Namespace

Modules: EventDispatcher, ObjectSpecifier Classes: DocItem, DocMethod, Element, ElementList, ElementRecord, Enumerator, ObjectSpecifierList

Class Method Summary collapse

Class Method Details

.__convert_to_osa__(requested_type, value, enum_group_codes = nil) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/rubyosa/rbosa.rb', line 365

def self.__convert_to_osa__(requested_type, value, enum_group_codes=nil)
  return value if value.is_a?(OSA::Element)
  if conversion = @conversions_to_osa[requested_type]
    args = [value, requested_type]
    conversion.call(*args[0..(conversion.arity - 1)])
  elsif enum_group_codes and enum_group_codes.include?(requested_type)
    if value.is_a?(Array)
      ary = value.map { |x| OSA::Element.__new__('enum', x.code.to_4cc) } 
      ElementList.__new__(ary)
    else
      ['enum', value.code.to_4cc]
    end
  elsif md = /^list_of_(.+)$/.match(requested_type)
    ary = value.to_a.map do |elem| 
      obj = convert_to_osa(md[1], elem, enum_group_codes) 
      obj.is_a?(OSA::Element) ? obj : OSA::Element.__new__(*obj)
    end
    ElementList.__new__(ary)
  else
    STDERR.puts "unrecognized type #{requested_type}" if $VERBOSE
    ['null', nil]
  end
end

.__four_char_code__Object

.__remote_processes__Object

.__scripting_info__Object

.add_conversion(hash, types, block, max_arity, replace = false) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/rubyosa/rbosa.rb', line 332

def self.add_conversion(hash, types, block, max_arity, replace=false)
  raise "Conversion block has to accept either #{(1..max_arity).to_a.join(', ')} arguments" unless (1..max_arity) === block.arity
  types.each do |type|
    next if !replace and hash.has_key?(type)
    hash[type] = block 
  end
end

.add_conversion_to_osa(*types, &block) ⇒ Object



352
353
354
# File 'lib/rubyosa/rbosa.rb', line 352

def self.add_conversion_to_osa(*types, &block)
  add_conversion(@conversions_to_osa, types, block, 2)
end

.add_conversion_to_ruby(*types, &block) ⇒ Object



344
345
346
# File 'lib/rubyosa/rbosa.rb', line 344

def self.add_conversion_to_ruby(*types, &block)
  add_conversion(@conversions_to_ruby, types, block, 3)
end

.add_property(sym, code, override = false) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubyosa/rbosa_properties.rb', line 33

def self.add_property(sym, code, override=false)
  unless override
    return if @sym_to_code.has_key?(sym) or @code_to_sym.has_key?(code)
  end
  @sym_to_code[sym] = code
  @code_to_sym[code] = sym
end

.app(*args) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/rubyosa/rbosa.rb', line 306

def self.app(*args)
  if args.size == 2
    if args.first.is_a?(String) and args.last.is_a?(Hash)
      hash = args.last
      if hash.has_key?(:name)
        warn "Given Hash argument already has a :name key, ignoring the first String argument `#{args.first}'"
      else
        hash[:name] = args.first
      end
      args = hash
    else
      raise ArgumentError, "when called with 2 arguments, the first is supposed to be a String and the second a Hash"
    end
  else
    if args.size != 1
      raise ArgumentError, "wrong number of arguments (#{args.size} for at least 1)"
    end
    args = args.first
  end
  args = { :name => args } if args.is_a?(String)
  self.__app__(*OSA.__scripting_info__(args))
end

.app_by_bundle_id(bundle_id) ⇒ Object



296
297
298
299
# File 'lib/rubyosa/rbosa.rb', line 296

def self.app_by_bundle_id(bundle_id)
  STDERR.puts "OSA.app_by_bundle_id() has been deprecated and its usage is now discouraged. Please use OSA.app(:bundle_id => 'bundle_id') instead."
  self.__app__(*OSA.__scripting_info__(:bundle_id => bundle_id))
end

.app_by_signature(signature) ⇒ Object



301
302
303
304
# File 'lib/rubyosa/rbosa.rb', line 301

def self.app_by_signature(signature)
  STDERR.puts "OSA.app_by_signature() has been deprecated and its usage is now discouraged. Please use OSA.app(:signature => 'signature') instead."
  self.__app__(*OSA.__scripting_info__(:signature => signature))
end

.app_with_name(name) ⇒ Object



286
287
288
289
# File 'lib/rubyosa/rbosa.rb', line 286

def self.app_with_name(name)
  STDERR.puts "OSA.app_with_name() has been deprecated and its usage is now discouraged. Please use OSA.app('name') instead."
  self.__app__(*OSA.__scripting_info__(:name => name))
end

.app_with_path(path) ⇒ Object



291
292
293
294
# File 'lib/rubyosa/rbosa.rb', line 291

def self.app_with_path(path)
  STDERR.puts "OSA.app_by_path() has been deprecated and its usage is now discouraged. Please use OSA.app(:path => 'path') instead."
  self.__app__(*OSA.__scripting_info__(:path => path))
end

.code_to_sym(code) ⇒ Object



45
46
47
# File 'lib/rubyosa/rbosa_properties.rb', line 45

def self.code_to_sym(code)
  @code_to_sym[code]
end

.convert_to_osa(requested_type, value, enum_group_codes = nil) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/rubyosa/rbosa.rb', line 389

def self.convert_to_osa(requested_type, value, enum_group_codes=nil)
  ary = __convert_to_osa__(requested_type, value, enum_group_codes)
  if ary == ['null', nil]
    new_type = case value
      when String then 'text'
      when Array then 'list'
      when Hash then 'record'
      when Integer then 'integer'
      when Float then 'double'
    end
    if new_type
      ary = __convert_to_osa__(new_type, value, enum_group_codes)
    end
  end
  ary
end

.convert_to_ruby(osa_object) ⇒ Object



356
357
358
359
360
361
362
363
# File 'lib/rubyosa/rbosa.rb', line 356

def self.convert_to_ruby(osa_object)
  osa_type = osa_object.__type__
  osa_data = osa_object.__data__(osa_type) if osa_type and osa_type != 'null'
  if conversion = @conversions_to_ruby[osa_type]
    args = [osa_data, osa_type, osa_object]
    conversion.call(*args[0..(conversion.arity - 1)])
  end
end

.replace_conversion_to_osa(*types, &block) ⇒ Object



348
349
350
# File 'lib/rubyosa/rbosa.rb', line 348

def self.replace_conversion_to_osa(*types, &block)
  add_conversion(@conversions_to_osa, types, block, 2, true)
end

.replace_conversion_to_ruby(*types, &block) ⇒ Object



340
341
342
# File 'lib/rubyosa/rbosa.rb', line 340

def self.replace_conversion_to_ruby(*types, &block)
  add_conversion(@conversions_to_ruby, types, block, 3, true)
end

.set_params(hash) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/rubyosa/rbosa.rb', line 406

def self.set_params(hash)
  previous_values = {}
  hash.each do |key, val|
    unless OSA.respond_to?(key)
      raise ArgumentError, "Invalid key value (no parameter named #{key} was found)"
    end
    ivar_key = '@' + key.to_s
    previous_val = self.instance_variable_get(ivar_key)
    previous_values[ivar_key] = previous_val;
    self.instance_variable_set(ivar_key, hash[key])
  end
  if block_given?
    yield
    previous_values.each { |key, val| self.instance_variable_set(key, val) }
  end
  nil
end

.sym_to_code(sym) ⇒ Object



41
42
43
# File 'lib/rubyosa/rbosa_properties.rb', line 41

def self.sym_to_code(sym)
  @sym_to_code[sym]
end