Snippets code from my daily experience

October 20, 2008

Adding Komodo macros to context menu

Filed under: context menu,komodo,macro,onpopupshowing,xul — dafi @ 6:37 pm

Komodo macros allow to add new features to the editor, they are like little XUL extensions embedded into application.

I’ve written dozen of macros and many of them are strictly related to cursor position or selected text so it sounds reasonable to access them using the context menu but at this time Komodo doesn’t allow this.

Adding macros to context menu is easy, it can be done also using a ‘Trojan’ macro😀 triggered at Komodo startup but the best results are achieved using an extension containing configuration dialogs and other whistle and bells.

I’m lazy so I use one mega wrapper extension where I put any new code, a very quick and dirty practice.

Here I want to share the snippets necessary to add macros to context menu and running them selecting the corresponding menuitem.

For the sake of example the code shown below adds all macros found on toolbox, this can produce a very long (and unusable) menu.

It is necessary to install the onpopupshowing event handler so we can add/remove menu items dynamically.

onLoad : function() {
    var ctx = document.getElementById("editorContextMenu");
    ctx.addEventListener("popupshowing", this.onPopupShowing, false);

the onPopupShowing function accesses to toolbox and populates the context menu

onPopupShowing : function(event) {
    var arr = {};
    var count = {};

    // get all macros, very ugly but easy to customize
    ko.toolboxes.user.toolbox.getChildrenByType("macro", true, arr, count);

    // remove menu items
    var i = 0;
    var mi;
    for (var i = 0; mi = document.getElementById("macro2ctx" + i); i++) {

    var ctx = document.getElementById("editorContextMenu");
    for (var i = 0; i < count.value; i++) {
        var mi = document.createElement("menuitem");
        var file = arr.value[i].getFile();
        mi.setAttribute("id", "macro2ctx" + i);
        mi.setAttribute("label", file.leafName);
        mi.setAttribute("oncommand", "macro2Ctx.onRunMacro('" + file.displayPath + "')");

Finally the onRunMacro function executes the macro found starting from its URL

onRunMacro : function(url) {
var item = ko.toolboxes.user.toolbox.getChildByURL(url);

That’s all Folks

I hope to move all code on a separated extension and publish it…

but before

  • Macros to add to context menu must be choosen by user
  • Macros must be removed easily
  • Choose the context menu to use (tab context menu or editor context menu)
  • Allow to show macro only if it matches some criteria (like isCommandEnabled)
  • Better error handling

Blog at

%d bloggers like this: