# Kile: show me script example

Can someone please show me an example script in Kile?

I want a example.

.

.

.

.

-

First things first. :)

According to The Kile Handbook, chapter 13, "Kile's scripting feature allows for the execution of ECMAScript code. Scripts can be managed through the scripting panel in the sidebar." As a reference, both JavaScript and ActionScript are dialects of ECMAScript.

Unfortunately, my answer won't TeX-related. But here we go. :)

A quick look into the ECMAScript Language Specification plus the section 13.2 (API Reference) of The Kile Handbook made me come up with the following example:

1. I created a new script and called myscript.js with the content described as follows; Kile seems to save them in a specific scripts folder.

var myCurrentDoc = (kile).currentTextDocument()
myCurrentDoc.insertText("Hello " + myName)


In line 1, I get a KileTextDocument object which reflects the currently active text document. Please note that my code has no error checking! In line 2, the getInputValue method opens a dialog with the given caption and label; the variable myName gets the returned value. In line 3, I simply insert the text.

2. I saved the script and now it's available on the sidebar:

3. Let's say I create a dummy test.tex file with the following content:

\documentclass{article}

\begin{document}

| % suppose the cursor is here.

\end{document}


4. Running the script:

5. The result:

When clicking OK:

There we go! My first script in Kile. :)

Take a look in the API Reference. It might have some useful methods, but beware! According to the handbook:

Please note that the scripting API has not been finalized yet. The API described below might change in future versions of Kile.

Sorry to come up with this simple example, that's the only idea I had in such short time. :)

By the way: boas festas, amigo! :)

-
Perfect, this is already beginning to get me to touch it. Thank you. And Merry Christmas to you too. –  Regis da Silva Dec 25 '11 at 1:47
Now, how to find (or locate) text (CTRL+F), using this method? –  Regis da Silva Dec 25 '11 at 2:08
@Regis: Good question! I'm afraid there's no easy solution. :( Kile's scripting API seems to be promising, but as stated in the manual, it's under heavy development and likely to change in the future. IMHO the provided list of methods is quite limited. A possible workaround is to implement your own find method, that is, map the whole text to a data structure and manipulate lines and columns, and use one of Kile's methods to set the cursor position on-screen. I'm not sure if it's worth. As I see it, the scripting API provides just a basic interation experience. :( –  Paulo Cereda Dec 25 '11 at 9:47
But you did not say it gives to use javascript? Does it not to try this way? –  Regis da Silva Dec 25 '11 at 12:19
@Regis: Yes, you can use JS, but IMHO the problem resides in the document interface provided by the Kile API. The available methods are scarce, so there's nothing much to do. JS can help a lot, but you are always bounded by the API. Of course, I'm not very well versed in JS, so maybe anyone else might come up with an answer to this particular issue. But my programmer intuition tells me there's nothing we can do unless hacking the API implementation itself. –  Paulo Cereda Dec 25 '11 at 13:09