Python-based userscript instead of javascript-based

Background

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

Problem

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

Question

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.

rod

   "import eollib", 
    "from xpcom import components", 
    "", 
    "viewSvc = components.classes[\"@activestate.com/koViewService;1\"].getService(components.interfaces.koIViewService)", 
    "view = viewSvc.currentView", 
    "view = view.queryInterface(components.interfaces.koIScintillaView)", 
    "sm = view.scimoz", 
    "", 
    "wwatch = components.classes[\"@mozilla.org/embedcomp/window-watcher;1\"].getService(components.interfaces.nsIWindowWatcher)", 
    "prompt = wwatch.getNewPrompter(wwatch.activeWindow)", 
    "", 
    "# get cursor position", 
    "curs1 = sm.currentPos", 
    "", 
    "# copy buffer", 
    "sel1 = sm.setSel(1,5)", 
    "sm.documentStartExtend()", 
    "sm.documentEndExtend()", 
    "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", 
    "mod2v=[]", 
    "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", 
    "sm.setSel(start,end)", 
    "eol = eollib.eol2eolStr[sm.eOLMode]    ", 
    "sm.replaceSel(eol.join(mod2v))", 
    "", 
    "sm.gotoPos(curs1)"

There are a couple of examples here:

https://github.com/Komodo/macros

And there is some general discussion here:

http://docs.activestate.com/komodo/8.0/macroapi.html

I have not tried them on Komodo 11.

rod

@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["@activestate.com/koViewService;1"].getService(components.interfaces.koIViewService) 
view = viewSvc.currentView 
view = view.queryInterface(components.interfaces.koIScintillaView) 
sm = view.scimoz 
 
wwatch = components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(components.interfaces.nsIWindowWatcher) 
prompt = wwatch.getNewPrompter(wwatch.activeWindow) 
 
# get cursor position 
curs1 = sm.currentPos 
 
# copy buffer 
sel1 = sm.setSel(1,5) 
sm.documentStartExtend() 
sm.documentEndExtend() 
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 
mod2v=[] 
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 
sm.setSel(start,end) 
eol = eollib.eol2eolStr[sm.eOLMode]     
sm.replaceSel(eol.join(mod2v)) 
 
sm.gotoPos(curs1)

I have not run or test the above.

  • Carey

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

Rod