Skip to main content
Known Participant
June 3, 2010
Answered

Split Paragraph Into Multiple Text Frames

  • June 3, 2010
  • 1 reply
  • 8603 views

Hi everyone,

Was wondering if you guys could help me out. I'm trying to automate the process of taking each line in a paragraph (separated by forced line breaks) and giving it its own text frame, so that I can move them around independently. This is best illustrated in the screenshot attached.

Do you think this is possible via a script?

Thank you in advance!

Lewis

Message was edited by: GD_lewis

Correct answer Jongware

Okay, that image helped a lot in providing context!

Does this work for you? Select a number of words; these will be split by its regular spaces (I use a slightly different GREP, hope it's of no consequence), then moved into frames of their own alongside your current text frame.

Then it creates a new text frame and inserts tomorrow's lottery numbers ... oh wait, I'm still working on that.

Don't try running this from inside a table or a footnote or really anything at all more complicated than a simple text frame. It'll most likely do something but I'm not sure what.

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 reply

Jongware
Community Expert
Community Expert
June 3, 2010

Possible? Sure -- but it depends on how closely you want the slivers match the original text layout.

This script is based on my CopyCutter; it splits a text frame into its component lines.

//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.duplicate (LocationOptions.BEFORE,newf.lines[0]);

          if (i > 0)

               top = f.lines[i-1].baseline;

          else

               top = f.geometricBounds[0];

          bot = f.lines.baseline;

          newf.geometricBounds = [ top, f.geometricBounds[1], bot, f.geometricBounds[3] ];

     }

     f.remove();

}

Known Participant
June 3, 2010

[Jongware], as ever... you've hit the nail on the head.

Thank you!