Hello,
I want to “grab” the current line number of my active document.
I found a Userscript, in python, that does:
return inspect.currentframe().f_back.f_lineno
but… it grabs the number of the Userscript instead of my active (foreground) document.
How to fix this?
careyh
February 12, 2021, 5:22pm
2
Morning @quintijn ,
If you use a JS Userscript instead you can reference the Komodo JS API .
You’ll want ko/editor
lib (which I linked above).
var editor = require("ko/editor");
editor.getLineNumber(editor.getCursorPosition());
That should get you what you need. You can trial any JS in the JS console; View menu > Tabs & Sidebars > Console
Thank you, Carey. Can you also show me how to put the line number (as string) on the clipboard?
careyh
February 12, 2021, 9:24pm
4
This script now does the job, it copies the current line number to the clipboard. Please react when you see reason for improvements:
var editor = require("ko/editor");
var linenum;
linenum = editor.getLineNumber(editor.getCursorPosition());
var clipboard = require("sdk/clipboard");
clipboard.set(String(linenum));
Thanks, greetings, Quintijn
careyh
February 16, 2021, 5:51pm
6
Looks good to me @quintijn .
The only thing I’d change is that when using the String()
constructor you should have the new
operator in front of it.
Having said that, I’d normally call toString()
on the Number
type you have there; linenum.toString()
.
Thanks. As a side question: this is community.komodoide.com . I also just posted a few questions on community.activestate.com . Which one is preferable?
Quintijn
careyh
March 3, 2021, 5:18pm
8
@quintijn here is preferable but I’m answering your questions on the AS community right now so don’t worry about moving those or anything.