Skip to main content
Participant
March 7, 2016
Answered

Replace selected text by another TextFrame content

  • March 7, 2016
  • 1 reply
  • 997 views

Hi,

I'm trying to transform a selected text to something new, involving text displacement and paragraph styling using a jsx script. Right now, everything works great :

  • I grab the select text
  • I create a new temporary TextFrame
  • I generate my new paragraphs and apply styles to them in the new TextFrame

What I wasn't able to achieve yet is to replace the original seleted text by the temporary TextFrame content.

Any help would be more than welcome

Some part of my script :

myText = app.selection[0].contents;

... do some stuff


var myDocument = app.documents.item(0);

var myPage = myDocument.pages.item(0);

var myTextFrame = myPage.textFrames.add();


myTextFrame.geometricBounds = [0, -70, 150, 70];

myTextFrame.contents = "Paragraph1\rParagraph2...";


myTextFrame.paragraphs[0].appliedParagraphStyle = pStyleGroup.paragraphStyles.itemByName('Style1');

myTextFrame.paragraphs[1].appliedParagraphStyle = pStyleGroup.paragraphStyles.itemByName('Style2');

And now, in myTextFrame, I have my formated new paragraph, and I'd like to replace the original selected text by my TextFrame content (keeping the paragraph styles).

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Keep code logic and modify utilities:

var

    mSourceIdx = app.selection[0].index,

    mStory = app.selection[0].parentStory,

    mReplaceStuff = {

        elements: [

            ["Paragraph_1\r",  "Style_1"],

            ["Paragraph_2\r",  "Style_2"],

            ["Paragraph_X\r",  "Style_X"]

            ]},

    mTarget, cElement, cText, cParaStyle;

while (cElement = mReplaceStuff.elements.pop()) {

    cText = cElement[0];

    cParaStyle = app.activeDocument.paragraphStyles.item(cElement[1]);    // modify if styleGroups present in a structure

  mTarget = mStory.insertionPoints.item(mSourceIdx);

    mTarget.contents = cText;

    mTarget.paragraphs[0].appliedParagraphStyle = cParaStyle;

     }

app.selection[0].contents = "";

Use mReplaceStuff to prepare stuff to replace

Jarek

1 reply

Jump_Over
Legend
March 7, 2016

Hi,

It could be done directly:

var

  mSource = app.selection[0],

  mStory = mSource.parentStory,

  mTarget = mStory.insertionPoints.item(mSource.index);

  mText1ToReplace = "Paragraph1\r",

  mText2ToReplace = "Paragraph2\r",

  mParaStyle1 = app.activeDocument.paragraphStyles.item("Style1"),

  mParaStyle2 = app.activeDocument.paragraphStyles.item("Style2");

  mSource.contents = mText2ToReplace;

  mTarget.paragraphs[0].appliedParagraphStyle = mParaStyle2;

  mTarget.contents = mText1ToReplace;

  mTarget.paragraphs[0].appliedParagraphStyle = mParaStyle1;

Jarek

PS. Modify mParaStyle1 & 2 declaration if styleGroups exists

Participant
March 8, 2016

Thanks Jarek, it works. However, it doesnt any more when I have more than 2 paragraphs. The second one keeps being overridden.

Thanks !

Jump_Over
Jump_OverCorrect answer
Legend
March 8, 2016

Hi,

Keep code logic and modify utilities:

var

    mSourceIdx = app.selection[0].index,

    mStory = app.selection[0].parentStory,

    mReplaceStuff = {

        elements: [

            ["Paragraph_1\r",  "Style_1"],

            ["Paragraph_2\r",  "Style_2"],

            ["Paragraph_X\r",  "Style_X"]

            ]},

    mTarget, cElement, cText, cParaStyle;

while (cElement = mReplaceStuff.elements.pop()) {

    cText = cElement[0];

    cParaStyle = app.activeDocument.paragraphStyles.item(cElement[1]);    // modify if styleGroups present in a structure

  mTarget = mStory.insertionPoints.item(mSourceIdx);

    mTarget.contents = cText;

    mTarget.paragraphs[0].appliedParagraphStyle = cParaStyle;

     }

app.selection[0].contents = "";

Use mReplaceStuff to prepare stuff to replace

Jarek