Skip to main content
Known Participant
November 18, 2024
Question

Why does it jump one character to the right after running the script?

  • November 18, 2024
  • 1 reply
  • 151 views

Why does it jump one character to the right after running the script?
How do I make it so that it keeps the entire modified area ([GENERAL]) selected?

 

 

// Add a specific symbol to the selection area, just modify what's between the ”
app.selection[0].contents = "[" + app.selection[0].contents + "]";

 

 

 

This topic has been closed for replies.

1 reply

AlanGilbertson
Community Expert
Community Expert
November 18, 2024

The object app.selection[0].contents has a string length of seven characters, set by the initial selection. Your code increases the contents without increasing the length (7 characters, in this case).

 

Try this:

if (app.selection.length > 0 && app.selection[0].hasOwnProperty('contents')) {
    var selectedText = app.selection[0];
    var originalText = selectedText.contents;
    selectedText.contents = '[' + originalText + ']';

    // Adjust the selection to include the new brackets
    var startIndex = selectedText.insertionPoints[0].index;
    var endIndex = selectedText.insertionPoints[selectedText.insertionPoints.length - 1].index;
    selectedText.parentStory.characters.itemByRange(startIndex, endIndex + 1).select();
}