6 Replies Latest reply on Nov 25, 2014 4:45 AM by Henry 'Pi' James

    Find and move text to existing anchored frame

    Henry 'Pi' James

      I have a large text story spanning across many pages. It contains a number of paragraphs that have the following content:

       

      [anchor character for an existing, but empty anchored text frame][leading text containing NO tab][TAB][middle text containing NO tab][TAB][trailing text containing NO tab][anchor character for another existing, but empty anchored text frame]

       

      I need a script (preferably Javascript) that:

       

      1. Find any paragraph that starts with an anchor character;

      2. Find and cut the leading text between the anchor character and the first TAB after it;

      3. Paste the leading text into the text frame anchored at the beginning of the paragraph;

      4. In the same paragraph, find the trailing text between the second (and last) TAB and the second (and last) anchor character at the end of the paragraph;

      5. Paste the trailing text into the text frame anchored at the end of the paragraph;

      6. Repeat 1.-5. for all paragraph in the same story that starts with an anchor character.


      I am an advanced JS programmer, but new to CreativeSuite scripting. I've kind of figured out how to search for certain text, and I know that the `parent` of an anchored object is a `Character` - but now to I access the content of an anchored object from its parent character?


        • 1. Re: Find and move text to existing anchored frame
          Henry 'Pi' James Level 1

          I got it!

           

          function movetoanchored(find) {
            find.characters.itemByRange(1, -1).select();
            app.cut();
            var myStory = app.selection[0].parentStory;
            var myIndex = app.selection[0].index;
            var myAnchor = myStory.characters[myIndex - 1];
            var myFrame = myAnchor.textFrames[0];
            app.select(myFrame.parentStory.insertionPoints.item(0));
            app.paste();
          };
          
          
          function main() {
            var myDoc = app.activeDocument;
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = "^~a.*?\\t";
            while (true) {
              var myFinds = app.activeDocument.findGrep();
              if (myFinds.length == 0) return;
              movetoanchored(myFinds[0]);
            };
          };
          
          
          main();
          
          • 2. Re: Find and move text to existing anchored frame
            Jump_Over Level 5

            Hi,

             

            Does it work with your second TAB-text-ANCHOR sequence?

             

            You should change preferences for "\\t[^\\t]+~a$" and set second anchored frame contents as find.characters.itemByRange(0,-2)

            (last character is an Anchor parent of course)

             

            You could use text.move() method to omit UI selection and display procedures.

            While moving keep objects compatibility (texts2texts, words2words etc., ie:

            find.characters.itemByRange(1,-1).texts[0].move(LocationOptions.BEFORE, mFrame.texts[0])

             

            Jarek

            • 3. Re: Re: Find and move text to existing anchored frame
              Henry 'Pi' James Level 1

              Moving the trailing text works on the same principle as the leading text, so that's no problem.

               

              About using `move`: I'd prefer it over copy/paste of course, but this didn't work:

               

              function movetoanchored(find) {
                var myAnchor = find.characters[0].texts[0];
                var myText = find.characters.itemByRange(1, -1).texts[0];
                myText.move(LocationOptions.before, myAnchor.textFrames[0].texts[0])
              };
              

               

              It reports "Cannot move the text to the given location". Any idea why?

              • 4. Re: Find and move text to existing anchored frame
                Jump_Over Level 5

                Hi,

                 

                Possibly it tries to move anchored box with text all together.

                Try to replace characters with insertionPoints - I mean:

                myText = find.insertionPoints.itemByRange(1,-1).text[0];

                 

                Jarek

                • 5. Re: Re: Re: Find and move text to existing anchored frame
                  Henry 'Pi' James Level 1

                  Never mind, the sample text I used for testing had some problem. Now it works:

                   

                  function movetoanchored(find) {
                    var myText = find.characters.itemByRange(1, -1).texts[0];
                    var myFrame = find.characters[0].textFrames[0];
                    myText.move(LocationOptions.atBeginning, myFrame);
                  };
                  
                  • 6. Re: Re: Find and move text to existing anchored frame
                    Henry 'Pi' James Level 1

                    Further improved:

                     

                    function movetoanchored(find) {
                      var myText = find.characters.itemByRange(1, -1).texts[0];
                      var myFrame = find.characters[0].textFrames[0];
                      myText.move(LocationOptions.AT_BEGINNING, myFrame);
                    };
                    
                    function main() {
                      var myStory = app.selection[0].parentStory;
                      app.findGrepPreferences = app.changeGrepPreferences = null;
                      app.findGrepPreferences.findWhat = "^~a\\S+";
                      var myFinds = myStory.findGrep(true);
                      for (i = 0; i < myFinds.length; i++) movetoanchored(myFinds[i]);
                      Window.alert(myFinds.length + " moves performed");
                    };
                    
                    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "MoveToAnchored");