Skip to main content
hilukasz
Known Participant
July 30, 2013
Question

Break text area into text lines

  • July 30, 2013
  • 7 replies
  • 9397 views

I want to break text area with line breaks in it into seperate text lines using return as delineator. for example

"Line 1

Line 2

Line 3"

I would like as

"Line 1"

"Line 2"

"Line 3"

I could probably write something but curious if someone already wrote it. I found a few similar ones like

http://forums.adobe.com/thread/321610

but they seem to be broken for CC here is the script in case you're curious

/////////////////////////////////////////////////////////////////

//Divide TextFrame v.2.2 -- CS and up

//>=--------------------------------------

// Divides a multiline text field into separate textFrame objects.

// Basically, each line in the selected text object

// becomes it's own textFrame. Vertical Spacing of each new line is based on leading.

//

// This is the opposite of my "Join TextFrames" scripts which

// takes multiple lines and stitchs them back together into the same object.

// New in 2.1 now right and center justification is kept.

// New in 2.2 better error checking, and now will run on more than one text frame at a time.

//>=--------------------------------------

// JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com

//copyright full text here:  http://www.wundes.com/js4ai/copyright.txt

//////////////////////////////////////////////////////////////////

var doc = activeDocument;

var genError= "DivideTextFrame must be run on a point-text text-frame. ";

var ret_re = new RegExp("/[\x03]|[\f]|[\r\n]|[\r]|[\n]|[,]/");

if(doc){

        var docsel = doc.selection;

        var sel = [];

    //remember initial selection set

         for(var itemCt=0, len = docsel.length ;itemCt<len;itemCt++){

             if(docsel[itemCt].typename == "TextFrame"){

                  sel.push(docsel[itemCt]);

             }

         }

    

        if(sel.length){  //alert(sel.length+" items found.");

            for(var itemCt=0, len = sel.length ;itemCt<len;itemCt++){

                divide(sel[itemCt]);

            }     

        }else{

                alert(genError +"Please select a Text-Frame object. (Try ungrouping.)");

        }      

}else{

    alert(genError + "No document found.");

};

function divide(item){

   

          //get object position

    var selWidth = item.width;

if(item.contents.indexOf("\n") != -1){

          //alert("This IS already a single line object!");

}else{

       

    //getObject justification

    var justification = item.story.textRange.justification;

   

          //make array

          var lineArr = fieldToArray(item);

          tfTop = item.top;

          tfLeft = item.left;

          item.contents = lineArr[0];

          //for each array item, create a new text line

          var tr = item.story.textRange;

          var vSpacing = tr.leading;

    var newTF;

          for(j=1 ; j<lineArr.length ; j++){

                    newTF = item.duplicate(doc, ElementPlacement.PLACEATBEGINNING);

                    newTF.contents = lineArr;

                    newTF.top = tfTop - (vSpacing*j);

        if(justification == Justification.CENTER)

        {

             newTF.left = (tfLeft + (selWidth/2)) - (newTF.width/2);

        }

    else

            if(justification == Justification.RIGHT)

        {

            newTF.left = (tfLeft + selWidth) - newTF.width;

        }

    else

    {

           newTF.left = tfLeft;

    }

                    newTF.selected = false;

          }

}

function fieldToArray(myField) { 

                    retChars = new Array("\x03","\f","\r","\n");

                    var tmpTxt = myField.contents.toString();

                    for (all in retChars )

                    {

            tmpArr = tmpTxt.split(retChars[all]);

                    } 

                    return tmpTxt.split(ret_re);

          }

    }

7 replies

renél80416020
Inspiring
August 12, 2019

Salut!

      Je me suis permis de modifier le très bon script de John Wundes, dont la fonction "function fieldToArray" est super.

      J'utilse l'"objet line" pour remplir chaque nouveau texte de point, de plus je peux traiter les texte de type AREATEXT  

Je donne le script au prochain message...  

exemple comparatif

      J'ai ajouté une bordure à chaque objet pour bien les distinguer,
      vous constatez que dans les deux cas les attributs de caractères
      sont ceux de la première lettre du texte sélectionné.

Si vous voulez une version qui copie les "objets Character" de chaque "objet line" me contacter par mail.

Avec "leading" actualisé pour chaque ligne

exemple pour texte de point

      objet Character of objet line

De elleere

      PS A quel usage peut servir ce genre de script ?

renél80416020
Inspiring
August 12, 2019

le script modifié:

// JavaScript Document for Illustrator

var remove = true;

var doc = activeDocument;

var genError= "DivideTextFrame must be run on a point-text text-frame. ";

  if(doc){

    var docsel = doc.selection;

    var k = 0;

      if(docsel.length){  //alert(sel.length+" items found.");

        for(var itemCt=0, len = docsel.length ;itemCt<len;itemCt++){

          if(docsel[itemCt].typename == "TextFrame"){

            if(docsel[itemCt].lines.length == 1){

              //alert("This IS already a single line object!");

              continue;

            }

            else{

              divide(doc,docsel[itemCt]);

              k++;

            }

          }

        }

        if(k == 0)alert(genError +"Please select a Text-Frame object. (Try ungrouping.)")

      }

      else{

        alert(genError + "No document found.");

      }

}

//------------

function divide(relativeObjet,item){

  //getObject justification and leading

  var justification = item.textRange.justification;

  var vSpacing = item.textRange.leading;

  var tfTop = item.top;

  var tfLeft = item.left;

  var newTF;

    for(var j = 0; j < item.lines.length; j++){

      newTF = relativeObjet.textFrames.add();

      item.characters[0].duplicate(newTF.insertionPoints[0]);

      newTF.contents = item.lines.contents;

      newTF.top = tfTop;

      tfTop -= vSpacing;

        switch (justification) {

          case (Justification.CENTER): newTF.left = (tfLeft + (item.width/2)) - (newTF.width/2); break;

          case (Justification.RIGHT): newTF.left = (tfLeft + item.width) - newTF.width; break;

          default : newTF.left = item.left;

        }

        newTF.textRange.justification = justification;

    }

  if (remove) {item.remove();}

}

Participating Frequently
May 20, 2022

Hi, did you fix the formatting problem? The code above isn't working for me. Can you confirm if it still works? Many thanks!

Inspiring
August 10, 2019

I need to paste this in an .jsx document, right?
I get an error at line 18, "activeDocument is undefined".

Using InDesign 14.0.2

Tom Winkelmann
Inspiring
August 10, 2019

It's a script for AI - not for ID

kaseym75204165
Participating Frequently
March 21, 2019

Confirming that this works fine in CC 2019. Thanks for the script! If anyone knows a more user-friendly approach, holler! But, for now this is a great time saver.

Disposition_Dev
Legend
August 7, 2018

I just tested this script as is in CC 2018 and it works just fine.

rcraighead
Legend
August 7, 2018

Thanks, William. I'll give it another try in 2015 (we are standardized on 2015 for now).

rcraighead
Legend
August 7, 2018

Has anyone made this work in CC (2015)?

Participating Frequently
March 2, 2015

Great tool, save me a lot lot of time!! Thank you

Participating Frequently
June 13, 2014

+1

I need this too!!