14 Replies Latest reply on Feb 28, 2017 4:41 AM by Obi-wan Kenobi

    Editting CopyCutter to break at hard returns?

    codyhsmdr

      So I wanted to do what this guy wanted in this thread and break a long frame of text down to multiple text frames based on hard returns and he was told to use Copycutter and it seemed to work for him but I can only get it to create new text frames at each space not hard turn. I can't figure out how to read the script to change it. Could anyone help me please?

       

      2.png

       

      //DESCRIPTION:CopyCutter -- Run Me On A Text Frame To Shred It Into Strips
      // A Jongware script 7-Aug-2011
      // w/Thanks to SebastiaoV to find a working version
      
      
      if (app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline") && app.selection[0].length > 1)
      {
        app.selection[0].insertionPoints[0].contents = "\r";
        app.selection[0].insertionPoints[-1].contents = "\r";
        app.findGrepPreferences = null;
        app.changeGrepPreferences = null;
        app.findGrepPreferences.findWhat = " +";
        app.changeGrepPreferences.changeTo = "\\r";
        app.selection[0].changeGrep();
        p = app.selection[0].parentTextFrames[0];
        lh = (p.lines[-1].baseline - p.lines[0].baseline) / (p.lines.length-1);
        top = app.selection[0].lines[0].baseline - lh;
        while (app.selection[0].length > 0)
        {
        f = app.activeDocument.layoutWindows[0].activePage.textFrames.add ({geometricBounds:[top, p.geometricBounds[3]+2*lh, top+lh, 2*(lh+p.geometricBounds[3])-p.geometricBounds[1] ]});
        app.selection[0].lines[0].move (LocationOptions.AFTER, f.texts[0]);
        top += lh;
        }
        app.selection[0].insertionPoints[-1].contents = "";
      } else
        alert ("please select some text to shred");
      
        • 1. Re: Editting CopyCutter to break at hard returns?
          Michael Witherell Adobe Community Professional

          Did you try taking out lines 12 and 13?

          • 2. Re: Editting CopyCutter to break at hard returns?
            codyhsmdr Level 1

            Nope cause I dunno how to read this type of script , but i'll try that now!

            • 3. Re: Editting CopyCutter to break at hard returns?
              codyhsmdr Level 1

              Tried it and removing lines 12-13 also makes it work on soft returns, I need it to only work with hard returns. I'm trying to make it break at the ends of a paragraph instead of each line but using soft returns in the paragraph and hard returns at the end.. I thought that was the basis of the code... am i wrong?

              • 5. Re: Editting CopyCutter to break at hard returns?
                Michael Witherell Adobe Community Professional

                I have no idea of the basis of the code. I'm just guessing with you. I don't seem to know a GREP code for ignoring a soft return, however. I'm surprised that \r is finding soft returns. But what if you edited the Line 13 (which now might be line 11) to change from "\\r" to "\\r$" What happens if you try that?

                • 6. Re: Editting CopyCutter to break at hard returns?
                  codyhsmdr Level 1

                  No change that I can see

                  • 7. Re: Editting CopyCutter to break at hard returns?
                    Michael Witherell Adobe Community Professional

                    Then I suppose I would do a Find/Change for \n soft returns and replace them with spacebar spaces; ... then run the script where you removed the two lines.

                    • 8. Re: Editting CopyCutter to break at hard returns?
                      codyhsmdr Level 1

                      Oh i hope it doesn't come to that, I have 25 pages of size 9 font to do.......

                      • 9. Re: Editting CopyCutter to break at hard returns?
                        [Jongware] Most Valuable Participant

                        Ah that script again. Yeah ... unfortunately, it proves to be popular to lots of people – but everyone and his aunt seems to have slightly different demands, and so there are lots and lots of edited versions floating around. This one doesn't really look like mine as I remember it – it slices everything into words, rather than lines.

                         

                        I've found what appears to be the original proto-version online (well, a v.2) in the archives of Fabian (thank you!) on Github IDSnippets/splitTextFrame.jsx at master · fabianmoronzirfas/IDSnippets · GitHub , and this one does look a lot like the original. It does not depend on 'spaces' or 'returns', it literally slices and dices a frame into the actual lines that you currently see in that text frame. It does not rely on findGrep, it merely iterates over the current text lines, not matter for what reason they wrap to a new line. You can see that if one of the lines end with a soft hyphen, the line will be broken at that point (although the soft hyphen itself is not saved, because according to the Document Model there never was one to begin with).

                         

                        This should be good for you:

                         

                        //DESCRIPTION:CopyCutter 2 — Cut your Copy into Slivers
                        // Jongware, 3-Jun-2010
                        if (app.selection.length == 1 && app.selection[0] instanceof TextFrame)
                        {
                          f = app.selection[0];
                          for (i=0; i<f.lines.length; i++)
                          {
                          newf = f.duplicate();
                          newf.texts[0].remove();
                          f.lines[i].duplicate (LocationOptions.BEFORE,newf.lines[0]);
                          if (i > 0)
                          top = f.lines[i-1].baseline;
                          else
                          top = f.geometricBounds[0];
                          bot = f.lines[i].baseline;
                          newf.geometricBounds = [ top, f.geometricBounds[1], bot, f.geometricBounds[3] ];
                          }
                          f.remove();
                        }
                        
                        1 person found this helpful
                        • 10. Re: Editting CopyCutter to break at hard returns?
                          codyhsmdr Level 1

                          That new code gives me this error
                          Screen Shot 2017-02-27 at 4.11.13 PM.png

                          • 11. Re: Editting CopyCutter to break at hard returns?
                            Laubender Adobe Community Professional & MVP

                            Hi Cody,

                            you did not copy all of the code.

                            The last closing } is perhaps missing in your script file.

                             

                            Regards,
                            Uwe

                            • 12. Re: Editting CopyCutter to break at hard returns?
                              Laubender Adobe Community Professional & MVP

                              Hi Cody,

                              as I can see from the code, the script CopyCutter 2 Jongware digged out in reply 9 will not fit your needs presented in reply 4.

                               

                              Hi Jongware,

                              just tested your CopyCutter 2 script on a simple text frame with InDesign CS6, also CS5.5.

                               

                              It is working—every single line is cut, but the second line is at a wrong vertical position.
                              All consecutive lines are ok if you see the relative distance to line two.

                               

                              From left to right:

                              1. Original text frame

                              2. Script result on a duplicate of the frame showing the problem with the vertical positions.

                              3. The relative vertical distance from frame two to all the frames following is perfect.

                              Not so the distance from frame 1 to frame 2.

                               

                              CopyCutter2-TEST-InDesign-CS6.png

                              Depending what one likes to do with the single text frames after running the script this little flaw might be not relevant.

                               

                              I did not look closer to the code why the shift happens.

                              But I would suggest, that before doing the dups of the lines

                              1. All baseline values of the original text frame could be read out

                              2. The text frames could be moved according to differences found in the old and new values of baseline

                               

                              Fixed version CopyCutter 2.1 plus global undo added:

                               

                              /**
                              * @@@BUILDINFO@@@ CopyCutter2.1-SELECTED-TEXTFRAME.jsx !Version! Tue Feb 28 2017 09:16:09 GMT+0100
                              */
                              
                              /*
                              
                                  CopyCutter 2.1 | Fixed version by Uwe Laubender [UL]
                                  Tested with CS5.5 and CS6
                                 
                                  [x] FIXED : The second cut out text frame is on wrong vertical position.
                                                  All consecutive ones as well.
                                 
                                  Original code by:
                                  // Jongware, 3-Jun-2010
                                  posted at Adobe InDesign Forum by Jongware:
                                 
                                  9. Re: Editting CopyCutter to break at hard returns?
                                  [Jongware] Feb 27, 2017 11:05 PM (in response to codyhsmdr)
                                  https://forums.adobe.com/message/9359833#9359833
                              
                              */
                              
                              //DESCRIPTION:CopyCutter 2.1 — Cut your Copy into Slivers | SELECTED TEXT FRAME
                              
                              
                              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                              
                              // [UL] Undo for all the script action in one go:
                              app.doScript
                                  (
                                 
                                  runCopyCutterOnTextFrame,
                                  ScriptLanguage.JAVASCRIPT,
                                  [],
                                  UndoModes.ENTIRE_SCRIPT,
                                  "Run CopyCutter 2.1 on Text Frame | SCRIPT"
                                 
                                  );
                              
                              function runCopyCutterOnTextFrame()
                              {
                                  if (app.selection.length == 1 && app.selection[0] instanceof TextFrame) 
                                  { 
                                     
                                      var f = app.selection[0];
                                     
                                      // [UL] Some new vars we need for fixing a problem:
                                      var doc = app.documents[0];
                                      var allBaseLines = f.lines.everyItem().baseline;
                                      var newFramesIDs = [];
                                     
                                     
                                      for (i=0; i<f.lines.length; i++) 
                                      { 
                                          var newf = f.duplicate(); 
                                          newf.texts[0].remove(); 
                                          f.lines[i].duplicate (LocationOptions.BEFORE,newf.lines[0]); 
                                          if (i > 0)
                                          var top = f.lines[i-1].baseline; 
                                          else 
                                          var top = f.geometricBounds[0]; 
                                          var bot = f.lines[i].baseline; 
                                          newf.geometricBounds = [ top, f.geometricBounds[1], bot, f.geometricBounds[3] ];
                                         
                                          // [UL] Store the id of the new frame:
                                          newFramesIDs[newFramesIDs.length++] = newf.id;
                                      };
                                 
                                      // [UL] Move to original position comparing old with new baseline values.
                                      // [UL] Will NOT work if text is aligned to any baseline grid.
                                     
                                      for(var n=0;n<allBaseLines.length;n++)
                                      {
                                          var newf = doc.pageItems.itemByID(newFramesIDs[n]).getElements()[0];
                                          var currentBaseline = newf.lines[0].baseline;
                                          newf.move( undefined , [0 ,  allBaseLines[n] - currentBaseline ]);
                                      }
                                 
                                      f.remove(); 
                                  }
                              }
                              

                               

                               

                              Regards,
                              Uwe

                              1 person found this helpful
                              • 13. Re: Editting CopyCutter to break at hard returns?
                                Laubender Adobe Community Professional & MVP

                                Hi Cody,

                                 

                                I did a new version of the script I posted in reply 12.

                                The new version will cut out the text by paragraphs and not by lines.

                                It is based on Jongware's original code in reply 9.

                                 

                                Code CopyCutter 3-CutEveryParagraph :

                                Tested with InDesign CS5.5 and CS6 on Mac OSX 10.7.5.

                                Should also run in newer versions.

                                 

                                /**
                                * @@@BUILDINFO@@@ CopyCutter3-CutEveryParagraph-SELECTED-TEXTFRAME.jsx !Version! Tue Feb 28 2017 09:20:49 GMT+0100
                                */
                                
                                
                                /*
                                
                                    Special purpose:
                                    Cut every paragraph.
                                    
                                    CopyCutter 3-CutEveryParagraph | by Uwe Laubender [UL]
                                    Tested with CS5.5 and CS6
                                    
                                    Based on original code by:
                                    // Jongware, 3-Jun-2010
                                    posted at Adobe InDesign Forum by Jongware:
                                    
                                    9. Re: Editting CopyCutter to break at hard returns?
                                    [Jongware] Feb 27, 2017 11:05 PM (in response to codyhsmdr) 
                                    https://forums.adobe.com/message/9359833#9359833
                                
                                */
                                
                                //DESCRIPTION:CopyCutter 3-CutEveryParagraph — Cut your Copy into Slivers | SELECTED TEXT FRAME
                                
                                
                                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                                
                                // [UL] Undo for all the script action in one go:
                                app.doScript
                                    (
                                    
                                    runCopyCutter3OnTextFrame, 
                                    ScriptLanguage.JAVASCRIPT, 
                                    [],
                                    UndoModes.ENTIRE_SCRIPT, 
                                    "Run CopyCutter 3 - Cut Every Paragraph Of Text Frame | SCRIPT"
                                    
                                    );
                                
                                function runCopyCutter3OnTextFrame()
                                {
                                    if (app.selection.length == 1 && app.selection[0] instanceof TextFrame)  
                                    {  
                                        
                                        var f = app.selection[0];
                                        
                                        // [UL] Some new vars we need for fixing a problem:
                                        var doc = app.documents[0];
                                        var allFirstBaseLinesOfParagraphs = f.paragraphs.everyItem().lines[0].baseline;
                                        var newFramesIDs = [];
                                        
                                        var paragraphs = f.paragraphs.everyItem().getElements();
                                        var paraLength = paragraphs.length;
                                        
                                        
                                        for (i=0; i<paraLength; i++)  
                                        {  
                                            var newf = f.duplicate();  
                                            newf.texts[0].remove();
                                            paragraphs[i].duplicate ( LocationOptions.AFTER , newf.insertionPoints[0] );
                                            if (i > 0)
                                            var top = paragraphs[i-1].lines[0].baseline;  
                                            else  
                                            var top = f.geometricBounds[0];  
                                            var bot = paragraphs[i].lines[-1].baseline;
                                            newf.geometricBounds = [ top, f.geometricBounds[1], bot, f.geometricBounds[3] ];
                                            
                                            // [UL] Store the id of the new frame:
                                            newFramesIDs[newFramesIDs.length++] = newf.id;
                                        };
                                    
                                        // [UL] Move to original position comparing old with new baseline values.
                                        // [UL] Will NOT work if text is aligned to any baseline grid.
                                        
                                        for(var n=0;n<allFirstBaseLinesOfParagraphs.length;n++)
                                        {
                                            var newf = doc.pageItems.itemByID(newFramesIDs[n]).getElements()[0];
                                            var currentBaseline = newf.lines[0].baseline;
                                            newf.move( undefined , [0 ,  allFirstBaseLinesOfParagraphs[n] - currentBaseline ]);
                                        }
                                    
                                        f.remove();  
                                    }
                                }
                                

                                 

                                Here a screen where I tested the code:

                                 

                                CopyCutter3-TEST-InDesign-CS5.5.png

                                If you wonder why the heights of the text frames are in different sizes, I cannot tell.
                                Did not do an optimization on the text frame's heights.

                                 

                                Here the individual frames shown as cascade:

                                 

                                CopyCutter3-TEST-InDesign-CS5.5-ShowingCascadedResults.png

                                 

                                But with CS6 and above one could easily optimize the heights by InDesign's autofitting feature with text frames.

                                 

                                Regards,
                                Uwe

                                1 person found this helpful
                                • 14. Re: Editting CopyCutter to break at hard returns?
                                  Obi-wan Kenobi Adobe Community Professional

                                  Hi Uwe,

                                   

                                  "Cut on "\r"":

                                   

                                  app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Extracted Texts! …");
                                  
                                  
                                  function main() {
                                          
                                      if ( app.selection.length == 1 && app.selection[0] instanceof InsertionPoint )
                                          Split()
                                      else {
                                          alert("Place the cursor in a text frame!\r(^/) ;-)");
                                          exit();
                                          }
                                         
                                          function Split() {
                                  
                                  
                                          var myDoc = app.activeDocument,
                                          mySourceFrame = app.selection[0].parentTextFrames[0],
                                          myGB = mySourceFrame.geometricBounds,
                                          myPage = mySourceFrame.parent,
                                          myLayer = mySourceFrame.itemLayer,
                                          //-----------------------------------------------------------------------
                                          myFindWhat = "[^\\r]+";
                                          //-----------------------------------------------------------------------
                                  
                                  
                                          if (! myDoc.objectStyles.itemByName("mySel").isValid) {
                                              myDoc.objectStyles.add({ 
                                                  name: "mySel",
                                                  fillColor: "None",
                                                  strokeColor: "None",
                                                  enableTextFrameAutoSizingOptions: true,  
                                                  textFramePreferences: {  
                                                      autoSizingType: AutoSizingTypeEnum.HEIGHT_ONLY,  
                                                      autoSizingReferencePoint: AutoSizingReferenceEnum.BOTTOM_LEFT_POINT,  
                                                      useNoLineBreaksForAutoSizing: true,
                                                      textColumnMaxWidth: myGB[3] - myGB[1]
                                                      }
                                                  });
                                              }
                                         
                                          var mySave = mySourceFrame.duplicate();
                                          if (! myDoc.layers.item("mySave").isValid)  myDoc.layers.add({ name: "mySave" });
                                          mySave.itemLayer = myDoc.layers.item("mySave");
                                          mySave.itemLayer.visible = false;
                                  
                                  
                                          app.findGrepPreferences = app.changeGrepPreferences = null;
                                          app.findGrepPreferences.findWhat = myFindWhat;
                                          myTexts = mySourceFrame.findGrep(),
                                          T = myTexts.length;
                                              while (T--) {
                                                  var mySel = myTexts[T],
                                                  myBaseline = mySel.lines[-1].baseline;
                                                  if ( mySel.contents !== "" ) {
                                                      myTextFrame = myPage.textFrames.add({geometricBounds: [myBaseline-5, myGB[1], myBaseline, myGB[3]]});
                                                      myTextFrame.itemLayer = myLayer;
                                                      mySel.characters.itemByRange(0, mySel.characters.length-1).move(LocationOptions.after, myTextFrame.insertionPoints[0]);  
                                                      myTextFrame.applyObjectStyle(myDoc.objectStyles.item("mySel"));
                                                      var myTextFrame_GB = myTextFrame.geometricBounds;
                                                      myTextFrame.textFramePreferences.autoSizingType = AutoSizingTypeEnum.OFF ;
                                                      }
                                                  }
                                             
                                          mySourceFrame.remove();
                                          myDoc.activeLayer = myLayer;
                                          app.findGrepPreferences = null;
                                          
                                          }   
                                  }
                                  

                                   

                                   

                                  (^/)