How do you write to the Command Output Window?

Posted by ericp on 2007-11-05 15:59
OS: All / Any | Product: Komodo | tags: output window

How do I write some text to Komodo's Command Output Window from a macro?


We don't talk as much about how to present output from macros as we should.
Alert boxes certainly work, and are an easy tool for web developers, but we
all know they get old fast. As long as there's a command output window in
the UI, we might as well write output to that, right?

One of the reasons the docs don't talk about this window is that it was
designed long ago, without much thought to making it scriptable from
outside. The code is somewhat low-level, but works. It also works
from extensions, of course. One day there will probably be a
higher-level entry point in the public API. Until then a function
like this will work fine.

Let's dive into the code.

// Don't make this var, or it won't be visible afterwards.
append_to_command_output_window = function(str) {
    try {
        // First make sure the command output window is visible, false);
        // Second, make sure we're showing the output pane, not the error list pane.
        var deckWidget = document.getElementById("runoutput-deck");
        if (deckWidget.getAttribute("selectedIndex") != 0) {
        // Now find out which newline sequence the window uses, and write the
        // text to it.
        var scimoz = document.getElementById("runoutput-scintilla").scimoz;
        var prevLength = scimoz.length;
        var currNL = ["\r\n", "\n", "\r"][scimoz.eOLMode];
        var full_str = str + currNL;
        var full_str_byte_length = ko.stringutils.bytelength(full_str);
        var ro = scimoz.readOnly;
        try {
            scimoz.readOnly = false;
            scimoz.appendText(full_str_byte_length, full_str);
        } finally {
            scimoz.readOnly = ro;
        // Bring the new text into view.
        scimoz.gotoPos(prevLength + 1);
    } catch(ex) {
        alert("problems printing [" + str + "]:" + ex + "\n");

If you're following along, you can create a macro (called, maybe,
"append_to_command_output_window", but macro names are mainly for
labeling) and save this text in it.

Notice that this macro doesn't actually *do* anything. It defines
a function, in Komodo's global namespace. You can then access
that function from any other macro simply by invoking
append_to_command_output_window, as in this example:

append_to_command_output_window("my text");
append_to_command_output_window("1 2 3");
append_to_command_output_window(""); // append a blank line
append_to_command_output_window("one last line");

If you give this macro an "On startup" trigger, all your other
JavaScript macros will have automatic access to it.

jeff.griffiths | Mon, 2007-11-05 16:02

Something like this is a great candidate for extending the ko namespace. Maybe or something...


prezez | Sun, 2008-11-09 17:03

any ideas how to do it in Python?

ActiveState Staff
Mon, 2008-11-10 15:02

There is no way to access the DOM element (at least easily) that contains the output tab, so unless you can provide your python script the correct dom element (or scimoz element) then you cannot get write to it.

ActiveState Staff
Mon, 2010-04-19 14:11

Update: a Python method for accessing the command output tab was found, see here:

jh | Sat, 2010-04-17 22:02

This line:

if (deckWidget.getAttribute("selectedIndex") != 0) {

Should be actually like this:

if (deckWidget.getAttribute("selectedIndex") !== "0") {

I.e. it should show what it actually does.

ericp | Mon, 2010-04-19 09:14

While we have strict mode turned on for JavaScript, I have
to admit that no one on the team has developed the habit
of using the === and !== operators to do strict type-based
comparisons. Good call.

Note that the code is correct, in that it does what it's
supposed to do. It also lets us program to an abstraction
where a "selectedIndex" property is numeric, even if this
is a fiction.

- Eric

westonruter | Fri, 2012-03-09 11:16

When I try to run append_to_command_output_window("test") I get:

problems printing [test]:TypeError: deckWidget is null

It seems that things have changed in Komodo 7?