Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Break text area into text lines

Explorer ,
Jul 30, 2013 Jul 30, 2013

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);

          }

    }

TOPICS
Scripting
8.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Jun 12, 2014 Jun 12, 2014

+1

I need this too!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2015 Mar 02, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2018 Aug 07, 2018

Has anyone made this work in CC (2015)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2018 Aug 07, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2018 Aug 07, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 25, 2018 Sep 25, 2018

I just tried it but got an error at Line 63…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2018 Sep 26, 2018

there was probably an error along the way when copying and pasting. That line doesn't do anything, it just declares an undefined variable.

What does the error message say?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 26, 2018 Sep 26, 2018

Rather than copy paste I downloaded it from wundes.com

But I worked out the issue. The full error message said "Error 8705: Target layer cannot be modified

Line: 63

–> new TF = item.duplicate(doc, ElementPlacement.PLACEATBEGINNING);"

I was focussed on the Line 63 part but I should've focussed on the Target layer part. My working layer wasn't locked, but the one above was. I unlocked the layer above and then the script worked. Too late for the job yesterday but now I know…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2019 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 10, 2019 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 10, 2019 Aug 10, 2019

It's a script for AI - not for ID

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 12, 2019 Aug 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 ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 12, 2019 Aug 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();}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 20, 2022 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2022 May 21, 2022

Error 1238: REquired value is missing
Line: 75
-> newTF.contents = item.lines.contents;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 21, 2022 May 21, 2022

Change said line to

 newTF.contents = item.lines[j].contents;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2022 May 21, 2022

Thank you very much! That solved the problem with the script and I could test the suggested update!

 

I thought that the new script @renél80416020 shared would keep the formatting, but it doesn't. The formatting used on the first character of the first paragraph will be used for all the remaining text.

 

All the best!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 23, 2022 May 23, 2022
LATEST

Bonjour Lucas,

Par exemple:

separe.PNGexpand image

René

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines