use "word under cursor" in script

TXS - Help
Marcel
2014-01-10
2014-01-11
  • Marcel
    Marcel
    2014-01-10

    Hey ho!

    Currently i'm writting a script, that marks a word as a indexentry. But i have to select the word and then i can use the macro. I would like to have, that the word under the cursor is used. Therefor i found this feature requests https://sourceforge.net/p/texstudio/feature-requests/219/

    However the following code produces an error

    %SCRIPT
    idx = cursor.selectedText(WordUnderCursor)
    editor.write("\\index{"+idx+"}"+idx)
    cursor.clearSelection()
    

    The error

    Uncaught exception at line 1: ReferenceError: Can't find variable: WordUnderCursor
    Backtrace <anonymous>()@:1
    

    According to the feature requests the wordundercursor function is implemented. Can anybody help me?

    Thank you for your help.

     
  • Tim Hoffmann
    Tim Hoffmann
    2014-01-10

    There are two things:

    1. you mixed up selection and retrieving the text
    2. to access the cursor movement related constants, you have to use the cursorEnums variable

    This should work:

    cursor.select(cursorEnums.WordUnderCursor)
    idx = cursor.selectedText()
    
     
  • Marcel
    Marcel
    2014-01-11

    Thank you, works perfectly.

    Yesterday i'm slow on the uptake. It makes sense, that i have first to select the text and then retrieve.

    I wanted to add the script and other to the wiki page (https://sourceforge.net/apps/mediawiki/texstudio/index.php?title=Scripts), but i haven't the permission to edit this page.

     
    Last edit: Marcel 2014-01-11
    • Tim Hoffmann
      Tim Hoffmann
      2014-01-11

      Should now be possible

       
  • Marcel
    Marcel
    2014-01-11

    Thanks, done, closed.