5 Replies Latest reply on Jun 4, 2018 7:31 AM by Marc Autret

    sel.duplicate problem

    akiva_atwood

      I'm trying to copy blocks of text from one file to a series of new files - each block in it's own file,

       

      The following code works -- USUALLY. occasionally the text in the new file is truncated in the middle (with no pattern as to when it is truncated.

       

      the alert shows the complete text block so I know the sel is correct

       

      any ideas as to what could be causing the problem?

       

      thanks

       

       

      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat=" newblock";

          found=app.activeDocument.findGrep();   

         

          for(i=0;i<found.length-1;i++){

              ipStart=found[i].index;

              ipEnd=found[i+1].index-1;

       

              sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd);

             //alert(i+"\r"+sel.contents);

            

              nd=makeNewDoc();// creates new doc - sets margins

              ntf=nd.pages[0].textFrames.add();

              gb=ntf.geometricBounds;

              gb[2]=gb[3]=200;

              gb[0]=gb[1]=0;

              ntf.geometricBounds=gb;

             

             sel.duplicate(LocationOptions.AT_BEGINNING,ntf);    

             }

       

        • 1. Re: sel.duplicate problem
          yadavs92328 Level 1

          Hi there,

                    If you are more concerned about text only, then instead of grep you should try text search, so your code should be like this,

          app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          app.findTextPreferences.findWhat=" newblock";
          found=app.activeDocument.findText();
          for(i=0;i<found.length-1;i++){
              ipStart=found[i].index;
              ipEnd=found[i+1].index-1;
              sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd);
              nd=makeNewDoc();
              ntf=nd.pages[0].textFrames.add();
              gb=ntf.geometricBounds;
              gb[2]=gb[3]=200;
              gb[0]=gb[1]=0;
              ntf.geometricBounds=gb;
              sel.duplicate(LocationOptions.AT_BEGINNING,ntf);
              }
          
          • 2. Re: sel.duplicate problem
            akiva_atwood Level 1

            Hi


            I need to use grep -- "new block" is preceded by numbers that have to be included. I left that out because it doesn't affect the script example

             

             

            Thanks

            • 3. Re: sel.duplicate problem
              akiva_atwood Level 1

              Follow up experimenting: if I make the textFrame huge (so the entire text block fits into the frame) then it seems to work properly...

               

              Which doesn't make any sense (as far as I can see)

               

              Any ideas why?

               

              thanks

              • 4. Re: sel.duplicate problem
                yadavs92328 Level 1

                As per your need I can understand, I think you should try this

                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;  
                app.findGrepPreferences.findWhat="newblock";  
                found=app.activeDocument.findGrep();  
                var ipStart = null;
                var ipEnd = null;
                for(i=0; i < found.length; i++){  
                    if(i==0){
                        ipStart = found[i].characters[0].index;
                        }
                    else{
                        if(i < found.length-1){
                            ipEnd=found[i].characters[0].index-1;
                            }
                        else if(i < found.length){
                            //******you can choose here if you want to add character after last newblock to the end of your story or not******//
                            ipEnd=found[i].parent.characters[found[i].parent.characters.length-1];
                            }
                        sel = found[0].parentStory.characters.itemByRange(ipStart,ipEnd);  
                        var nd = app.documents.add();
                        ntf=nd.pages[0].textFrames.add();
                        gb=ntf.geometricBounds;
                        gb[2]=gb[3]=200;
                        gb[0]=gb[1]=0;
                        ntf.geometricBounds=gb;
                        sel.duplicate(LocationOptions.AT_BEGINNING,ntf);
                        if(i < found.length-1){
                            ipStart = found[i].characters[0].index;
                            }
                        }
                    }
                
                • 5. Re: sel.duplicate problem
                  Marc Autret Level 5

                  Hi,

                   

                  What if you duplicate the text into ntf.parentStory (instead of ntf)?

                   

                  @+

                  Marc