[texworks] a script question

Paul A Norman paul.a.norman at gmail.com
Wed Mar 30 00:56:55 CEST 2011

Hi Pavol,

Ok you've got a few options. I'll focus on TeXworks here and its
scripting api and not underlying Qt framework options that might also

As the user has to do something anyway ...

If you tell the user to write their word and then double click it, you
have it selected already.

Following your approach ...

When you say the in front of the cursor that is describing the
situation where the user has just written a word and the cursor is now
at the end of that word?


If you are guaranteeing that there is a space before  envname  then
have a look at some rough documentation for the Tw scripting that we
use, its very much a draft and  incomplete as yet, but may help
getting you and anyone else going  ... (note for any references to
TW.app.system see the section

Looking at http://twscript.paulanorman.com/docs/index.html
(downloadable copies) and
in particularly on line web version at

"Check out script basics" and subsections, and "Script Authoring" and

 And here is a script for setting up  new scripts that might help ...



For your project ...

In the script information on line web version at
http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.htm open
up the section  "Tw Api functions and  properties" (click + symbol -
sorry this should be obvious)

Open up the section TW

Text stuff is mostly handled by "target" which represents the current
Tw document.

Open up the section "target"
Open up the section "text"

Tw itself provides direct methods (distinct form the Qt framework) for
reporting and handling the cursor position and making selections.

Then look at the entries with "select" as part of their name.

In QtScript (JavaScript) store the current cursor position (at the end
of the just typed envname).

  var endOfEnvName = TW.target.selectionStart;

Use the selection functions to move the selection point back setting a
range of 1

TW.target.selectRange(int, int)  where the first integer is the
absolute position form the start of the document, and the second
integer is the length you want here 1 , then you can obtain that from
script as TW.target.selection and see if it is a space " ".

Then select your envanme text using TW.target.selectRange(int, int) again.

I am sure you can work out the rest .. using
TW.target.insertText(AString) once you have selected and processed in
script  the envname.

Positioning the cursor in the middle? ... just in script reckon the
number of characters back from where the inserted text ends that you
want the cursor to end up and use TW.target.selectRange(int) notice
only the absolute position is required here.

However there might be an easier way ...

Using a short-cut key combo, start the script asking for some input
form the user getting the environment name they want.

This can be done by using a prompt type box...

Open up the section  "Tw Api functions and  properties" / TW / get User Input
then click on Tw.getText(QWidget*,QString,QString)

This can obtain the environment name directly from the user, and then
you can construct your string and use  TW.target.insertText(AString)
to insert it where the cursor is (it would replace any selected text
as well), then the calculated use of TW.target.selectionStart; and
TW.target.selectRange(int) to get into the middle of the environement.

You could also look at using the TwApi file read and write functions
to store the unique environment names the user uses over time and
present them in an editable drop-down box for selection or writing a
new one (and so storing it as well)

Open up the section  "Tw Api functions and  properties" / TW / File Functions
Open up the section  "Tw Api functions and  properties" / TW / get User Input
then click on   Tw.getItem get List Choice

Any way please do get back to the list if you have any other questions
and please do post any script you make on
http://code.google.com/p/texworks/issues/detail?id=261 for other to
use if you want to.


On 29 March 2011 22:16, Pavol Severa <pavol.severa at gmail.com> wrote:
> sorry
>> if xcvb is the word right between the cursor
> I wanted to say "in front of the cursor"

More information about the texworks mailing list