Module: OSA::EventDispatcher

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

Constant Summary collapse

SCRIPTING_ADDITIONS_DIR =
[
  '/System/Library/ScriptingAdditions',
  '/Library/ScriptingAdditions'
]

Instance Method Summary collapse

Instance Method Details

#__send_event__Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'ext/rubyosa/rbosa.c', line 265

static VALUE
rbosa_app_send_event (VALUE self, VALUE event_class, VALUE event_id, VALUE params, VALUE need_retval)
{
    OSErr       error;
    AppleEvent  ae;
    AppleEvent  reply;
    VALUE       rb_timeout;
    SInt32      timeout;
    VALUE       rb_reply;
    unsigned    has_direct_param;

    error = AECreateAppleEvent (RVAL2FOURCHAR (event_class),
                                RVAL2FOURCHAR (event_id),
                                rbosa_element_aedesc (self),
                                kAutoGenerateReturnID,
                                kAnyTransactionID,
                                &ae);
    if (error != noErr)
        rb_raise (rb_eArgError, "Cannot create Apple Event '%s%s' : %s (%d)", 
                  RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error);

    has_direct_param = 0;
    if (!NIL_P (params)) {
        unsigned    i;

        for (i = 0; i < RARRAY_LEN(params); i++) {
            VALUE   ary;
            VALUE   type;
            VALUE   element;
            FourCharCode code;

            ary = RARRAY_PTR(params)[i];
            if (NIL_P (ary) || RARRAY_LEN(ary) != 2)
                continue;

            type = RARRAY_PTR(ary)[0];
            element = RARRAY_PTR(ary)[1];
            code = RVAL2FOURCHAR (type);

            if (code == '----')
                has_direct_param = 1;

            error = AEPutParamDesc (&ae, RVAL2FOURCHAR (type), rbosa_element_aedesc (element));
            if (error != noErr) { 
                AEDisposeDesc (&ae); 
                rb_raise (rb_eArgError, "Cannot add Apple Event parameter '%s' : %s (%d)", 
                          RVAL2CSTR (type), error_code_to_string (error), error);
            }
        } 
    }

    rb_timeout = rb_iv_get (mOSA, "@timeout");
    timeout = NIL_P (rb_timeout) ? kAEDefaultTimeout : NUM2INT (rb_timeout);

    if (has_direct_param == 0)
        AEPutAttributePtr (&ae, 'subj', typeNull, NULL, 0);

    error = AESend (&ae, &reply, (RVAL2CBOOL(need_retval) ? kAEWaitReply : kAENoReply) | kAECanInteract | kAECanSwitchLayer,
                    kAENormalPriority, timeout, NULL, NULL);

    AEDisposeDesc (&ae); 

    if (error != noErr)
        rb_raise (rb_eRuntimeError, "Cannot send Apple Event '%s%s' : %s (%d)", 
                  RVAL2CSTR (event_class), RVAL2CSTR (event_id), error_code_to_string (error), error);

    __rbosa_raise_potential_app_error (&reply);

    if (RTEST (need_retval)) {
        AEDesc  replyObject;

        AEGetParamDesc (&reply, keyDirectObject, typeWildCard, &replyObject);

        rb_reply = rbosa_element_make (cOSAElement, &replyObject, self);
    }
    else {
        rb_reply = Qnil;
    }

    AEDisposeDesc (&reply);
        
    return rb_reply;
}

#merge(args) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rubyosa/rbosa.rb', line 256

def merge(args)
  args = { :name => args } if args.is_a?(String)
  by_name = args[:name]
  begin
    name, target, sdef = OSA.__scripting_info__(args)
  rescue RuntimeError => excp
    # If an sdef bundle can't be find by name, let's be clever and look in the ScriptingAdditions locations.
    if by_name 
      args = SCRIPTING_ADDITIONS_DIR.each do |dir|
        path = ['.app', '.osax'].map { |e| File.join(dir, by_name + e) }.find { |p| File.exists?(p) }
        if path
          break { :path => path }
        end
      end 
      if args.is_a?(Hash)
        by_name = nil
        retry
      end
    end
    raise excp
  end
  app_module_name = self.class.name.scan(/^OSA::(.+)::.+$/).flatten.first
  app_module = OSA.const_get(app_module_name) 
  OSA.__load_sdef__(sdef, target, app_module, true, self.class)
  (self.__send_event__('ascr', 'gdut', [], true) rescue nil) # Don't ask me why...
  return self 
end