Python-based userscript instead of javascript-based


There are some links out there indicating that Python can be used in KOIDE instead of Javascript.


There do not seem to be many (any?) examples with real python code. I have not found any.


Where is the best place to go to find examples and docs on python userscripts (not python programs or sourcecode, but actual, runnable userscripts)

There is no documentation for Python userscripts unfortunately. Generally I would not recommend using it as it serves a much different purpose than JS does. Python in Komodo is used for heavy lifting tasks, whereas JS carries the brunt of the UX tasks.

As for examples, the best place to look is the Komodo Edit source code. Python userscripts run against the Komodo runtime, so you have full access to the code us developers use also.

As nathanr said, there aren’t many (any?) Python macro examples around. Below is one that I wrote based on some code I found a while ago. This code automates some edits in the active edit window. Maybe you can get some ideas from it that are useful. The quotes and commas should be removed - they are there because I copied the code from an archive .komodotool file.


   "import eollib", 
    "from xpcom import components", 
    "viewSvc = components.classes[\";1\"].getService(components.interfaces.koIViewService)", 
    "view = viewSvc.currentView", 
    "view = view.queryInterface(components.interfaces.koIScintillaView)", 
    "sm = view.scimoz", 
    "wwatch = components.classes[\";1\"].getService(components.interfaces.nsIWindowWatcher)", 
    "prompt = wwatch.getNewPrompter(wwatch.activeWindow)", 
    "# get cursor position", 
    "curs1 = sm.currentPos", 
    "# copy buffer", 
    "sel1 = sm.setSel(1,5)", 
    "start = sm.positionFromLine(sm.lineFromPosition(sm.selectionStart))", 
    "end = sm.getLineEndPosition(sm.lineFromPosition(sm.selectionEnd))", 
    "mod1v = sm.getTextRange(start,end).splitlines()", 
    "#prompt.alert(\"length\", str(len(mod1v)))", 
    "# modify section lines", 
    "idx = 0", 
    "for ln in mod1v:", 
    "    if ln[0:3] == '[s]' :", 
    "        idx += 1", 
    "        ln = str(idx).zfill(2) + ln", 
    "        #prompt.alert(\"section\", ln)", 
    "    mod2v.append(ln)", 
    "# replace buffer", 
    "eol = eollib.eol2eolStr[sm.eOLMode]    ", 

There are a couple of examples here:

And there is some general discussion here:

I have not tried them on Komodo 11.


@rholland, It looks like you took the code directly out of your *.komodotool file. You should open the tool in Komodo and either take the text from the Properties windows or Edit the tool. I’ve removed the erroneous JSON quoting and commas.

import eollib 
from xpcom import components 
viewSvc = components.classes[";1"].getService(components.interfaces.koIViewService) 
view = viewSvc.currentView 
view = view.queryInterface(components.interfaces.koIScintillaView) 
sm = view.scimoz 
wwatch = components.classes[";1"].getService(components.interfaces.nsIWindowWatcher) 
prompt = wwatch.getNewPrompter(wwatch.activeWindow) 
# get cursor position 
curs1 = sm.currentPos 
# copy buffer 
sel1 = sm.setSel(1,5) 
start = sm.positionFromLine(sm.lineFromPosition(sm.selectionStart)) 
end = sm.getLineEndPosition(sm.lineFromPosition(sm.selectionEnd)) 
mod1v = sm.getTextRange(start,end).splitlines() 
#prompt.alert(\"length\", str(len(mod1v))) 
# modify section lines 
idx = 0 
for ln in mod1v: 
    if ln[0:3] == '[s]' : 
        idx += 1 
        ln = str(idx).zfill(2) + ln 
        #prompt.alert(\"section\", ln) 
# replace buffer 
eol = eollib.eol2eolStr[sm.eOLMode]     

I have not run or test the above.

  • Carey

Yep. I mentioned it above. Thanks for the cleanup.