Grab line number of foreground document

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?

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

  • Carey

Thank you, Carey. Can you also show me how to put the line number (as string) on the clipboard?

Hi @quintijn,

Clipboard contents into a variable? should answer your question.

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

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

@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.