Skip navigation
Currently Being Moderated

JavaScript To Transpose Words

Aug 25, 2011 2:43 PM

I am trying to write a JavaScript to transpose adjacent words.


I plan to use the "move" function but am having trouble getting a reference to the word under the cursor.


I can find out which character I am in within the story...but don't think I should need to iterate through all of the characters in the story to determine the word number.


Can someone provide some guidance on how I can determine the number of the word under the cursor?


Thanks much!

  • Currently Being Moderated
    Aug 26, 2011 6:04 AM   in reply to AaronKuehn

    app.selection[0].words[0] gives you a reference to the word.


    You can use story.words.previousItem(word) to get the previous one.


    It's probably faster to work it out based on character indexes though.


    Something like this:


    var sel = app.selection[0];

    var story = sel.parentStory;

    var curWord = sel.words[0];

    var curWordIndex = curWord.index;

    prevWord = story.characters.item(curWordIndex-2).words[0];


    You can then use move to move the words around, but you will probably need to deal with the spaces as well...



    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Aug 26, 2011 8:06 PM   in reply to AaronKuehn

    Yes, that is why you use characters[978].words[0] rather than words[978].

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points