4 Replies Latest reply on Mar 8, 2016 4:00 AM by yannickl67432203

    Replace selected text by another TextFrame content

    yannickl67432203

      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).

        • 1. Re: Replace selected text by another TextFrame content
          Jump_Over Level 5

          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

          • 2. Re: Replace selected text by another TextFrame content
            yannickl67432203 Level 1

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

             

            Thanks !

            • 3. Re: Replace selected text by another TextFrame content
              Jump_Over Level 5

              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

              • 4. Re: Replace selected text by another TextFrame content
                yannickl67432203 Level 1

                Thanks a lot Jarek, you rock !