macro running macro

Posted by jeff.griffiths on 2008-08-26 15:31

Here's a quick bit of code I've had around for a while that is useful when writing macros:

/*
 * a quick macro that tries to run the current macro open in the buffer
 * should be *not a macro*
*/

try {
  var view = ko.views.manager.currentView;
  var scimoz = view.scimoz;
  var uri = view.document.displayPath;
  var self = ko.macros.current;
 
  if(/^macro:\/\//.test(uri) && uri !== self.url) { // we don't want to run ourself
    var partName = (view.document.baseName.split('.').shift());
    var project = ko.projects.manager.currentProject;
    var part = ko.projects.findPart('macro', partName, '*', project);
    if(typeof(part.invoke) !== 'undefined') {
      part.invoke();
    } else {
      ko.projects.invokePart(part);
    }
  } else {
    if (uri == self.url) {
      msg = 'FAIL: you shouldn\'t try to run this macro *with* this macro.';
    } else {
      msg = 'Not a macro buffer, nothing to do...';
    }
    ko.statusBar.AddMessage(msg, 'editor', 3000);
  }
} catch(e) {
  ko.dialogs.internalError(e, 'Error: '+e);
}

This code runs the current buffer as a Macro, if the current buffer a) has macro:// uri scheme, and b) isn't itself. One important tidbit I just added is this:

if(typeof(part.invoke) !== 'undefined') {
  part.invoke();
} else {
  ko.projects.invokePart(part);
}

This code works around api differences between Komodo 4 and Komodo 5; in Komodo 5 the koIPart for macros no longer has an invoke method, so you need to pas the macro part into ko.projects.invokePart() instead to run it.