So you've written a Komodo macro, and find it isn't behaving exactly
as you had expected. Maybe you're hitting an off-by-one error working
with the scimoz editor widget, or you're grappling with high-bit
characters. You'd like to try out your code interactively, but given
that it's running in a specialized environment, you can't just fire up
a JavaScript shell, right?

Here's a way around that problem. This article is going to combine
two articles I wrote recently. The first one showed how to access the
Mozilla clipboard object to access the system clipboard, in
http://blogs.activestate.com/ericp/2007/10/index.html. The second was
on how to use Ted Mielczarek's Extension Developer Extension to
examine Firefox event objects after they run
(see http://blogs.activestate.com/ericp/2008/01/exploring-firef.html).

It turns out that you can use the Extension Developer Extension with
Komodo as well. Todd Whiteman modified it so it
will run with Komodo -- get this version at --
http://community.activestate.com/komodo-extension/komodo-developer-extension.
About all he did was modify the install.rdf file so Komodo would load
it -- the advantages of building an app on Mozilla. He also added a
separate Python shell, but I'll focus on JavaScript in this article.

The technique I used in the second post I referenced was to save the
parts of your macro that you're interested in by copying them into
global variables. This is JavaScript: any variable you don't declare
in your macro will have global visibility (and yes, you can
communicate between different macros this way, but writing an
extension is more robust).

The leading plus sign marks the mod to the macro:

    var aclipboard = new Clipboard();
+    gclip = aclipboard;
    var url = aclipboard.get();

Now bring up the JavaScript Shell with the [Tools|Extension
Developer|JavaScript Shell] menu command.

Click on "enumerateWindows", and then click on
"chrome://komodo/content/komodo.xul"

The next shot shows that the shell knows about the
Clipboard object we saved a reference to, and we can
use it.

Finally, if I switch back to the main editor and press
paste, the text I set the clipboard object to appears.
This is in your system clipboard, so it will work with
other applications as well.

Finally, let's use the shell to find that string and
remove it:

First get a handle to the editor object (scimoz) with this
code. Remember this widget has tab completion:

var sm = ko.views.manager.currentView.scimoz

Now you can get rid of the text by typing a simple

sm.undo()

But that won't give us a chance to interactively experiment
with scimoz. Let's put the text back and try it the long
way. I'll put the code here as text so you can copy and paste it in
to the shell, but feel free to explore as you go. You can find the
scimoz API at <komodo install dir%gt;/lib/sdk/idl/ISciMoz.idl.

sm.redo()

// Tell SciMoz the range of text to search in:
var needle = "// hello there"
var needleLen = needle.length
sm.targetStart = 0
sm.targetEnd = sm.length
var startPos = sm.searchInTarget(needleLen, needle)
var hitLine = sm.lineFromPosition(startPos);
var endPos = sm.positionFromLine(hitLine + 1);
sm.targetStart = startPos;
sm.targetEnd = endPos
sm.replaceTarget(0, "")

Sure, it's all a hack, but in the absense of a Komodo
debugger, it's a pretty useful hack.