Skip to main content
Community Expert
June 28, 2025
Answered

illustrator script – line break NOT working (after language)

  • June 28, 2025
  • 1 reply
  • 165 views

this is a simple script choosing and formating text from a nested array containing multiple languages. A dialog box makes the text parts adjustable. Everything works fine expect the little checkbox which should switch to add a simple line break after each language.

 

I wrapped my head around it for several hours now trying to approach from all sides but it just won't work.

Any thought any solution any one?

 

Thanks and regards

Nils

 

#target illustrator

function createMultilingualTextFrame() {
    var doc = app.activeDocument;

    var nestedArray = [
        [
            "Erster Punkt", "Zweiter Punkt", "Dritter Punkt", "Vierter Punkt", "Fünfter Punkt",
            "Sechster Punkt", "Siebter Punkt", "Achter Punkt", "Neunter Punkt", "Zehnter Punkt"
        ],
        [
            "First point", "Second point", "Third point", "Fourth point", "Fifth point",
            "Sixth point", "Seventh point", "Eighth point", "Ninth point", "Tenth point"
        ],
        [
            "Primer punto", "Segundo punto", "Tercer punto", "Cuarto punto", "Quinto punto",
            "Sexto punto", "Séptimo punto", "Octavo punto", "Noveno punto", "Décimo punto"
        ],
        [
            "Premier point", "Deuxième point", "Troisième point", "Quatrième point", "Cinquième point",
            "Sixième point", "Septième point", "Huitième point", "Neuvième point", "Dixième point"
        ]
    ];

    var includeIndices = [2, 5, 7];
    var formatIndices = [5];

    // Dialog box
    var dlg = new Window("dialog", "Textrahmen‑Optionen");
        dlg.orientation = "column";
        dlg.alignChildren = "left";
        dlg.add("statictext", undefined, "Horizontale Skalierung (%)");
    
    var scaleInput = dlg.add("edittext", undefined, "100");
        scaleInput.characters = 5;

        dlg.add("statictext", undefined, "Textrichtung");
    
    var alignDropdown = dlg.add("dropdownlist", undefined, ["Links bündig", "Blocksatz (letzte Zeile links)"]);
        alignDropdown.selection = 0;

    var wrapCb = dlg.add("checkbox", undefined, "Zeilenumbruch nach jeder Sprache einfügen");
        wrapCb.value = false;

    var btnGroup = dlg.add("group");
        btnGroup.alignment = "right";
        btnGroup.add("button", undefined, "OK");
        btnGroup.add("button", undefined, "Abbrechen");

    if (dlg.show() != 1) return;

    var hScale = parseFloat(scaleInput.text) / 100;
    var alignment = alignDropdown.selection.index;
    var wrap = wrapCb.value;

    function isInArray(v, a) {
        for (var i = 0; i < a.length; i++) if (a[i] === v) return true;
        return false;
    }

    function buildTextParts(texts) {
        var parts = [{ text: texts[0], originalIndex: 0 }];
        for (var i = 0; i < includeIndices.length; i++) {
            var idx = includeIndices[i];
            parts.push({ text: texts[idx], originalIndex: idx });
        }
        return parts;
    }

    function joinParts(parts) {
        var s = "";
        for (var i = 0; i < parts.length; i++) {
            s += parts[i].text;
            if (i < parts.length - 1) s += " ";
        }
        return s;
    }

    var allParts = [];
    var fullText = "";

    for (var i = 0; i < nestedArray.length; i++) {
        var texts = nestedArray[i];
        var parts = buildTextParts(texts);
        allParts = allParts.concat(parts);
        fullText += joinParts(parts);
        if (wrap && i < nestedArray.length - 1) {
            fullText += "\n"; // \r \n \n\n 
        } else if (i < nestedArray.length - 1) {
            fullText += " ";
        }
    }

    fullText = fullText.replace(/\s+/g, " ")
                       .replace(/#{3,}/g, ";")
                       .replace(/\. ,/g, ".");

    var rect = doc.pathItems.rectangle(-40, 10, 300, 200);
    var tf = doc.textFrames.areaText(rect);
        tf.contents = fullText;
        tf.textRange.characterAttributes.horizontalScale = hScale * 100;

    for (var i = 0; i < tf.paragraphs.length; i++) {
        if (alignment === 0) {
            tf.paragraphs[i].paragraphAttributes.justification = Justification.LEFT;
        } else {
            tf.paragraphs[i].paragraphAttributes.justification = Justification.FULLJUSTIFYLASTLINELEFT;
        }
    }

    function locatePositions(txt, parts) {
        var pos = [], cur = 0;
        for (var i = 0; i < parts.length; i++) {
            var st = parts[i].text.replace(/\s+/g, " ")
                                  .replace(/#{3,}/g, ";")
                                  .replace(/\. ,/g, ".");
            var start = txt.indexOf(st, cur);
            if (start < 0) start = cur;
            var end = start + st.length - 1;
            pos.push({ start: start, end: end, originalIndex: parts[i].originalIndex });
            cur = end + 1;
        }
        return pos;
    }

    var positions = locatePositions(fullText, allParts);
    var chars = tf.textRange.characters;

    for (var i = 0; i < positions.length; i++) {
        var p = positions[i];
        for (var j = p.start; j <= p.end && j < chars.length; j++) {
            var ca = chars[j].characterAttributes;
            if (p.originalIndex === 0) {
                ca.textFont = app.textFonts.getByName("Arial-ItalicMT");
                ca.size = 12;
            } else if (isInArray(p.originalIndex, formatIndices)) {
                ca.textFont = app.textFonts.getByName("Arial-BoldItalicMT");
                ca.size = 14;
            } else {
                ca.textFont = app.textFonts.getByName("ArialMT");
                ca.size = 12;
            }
        }
    }

}

createMultilingualTextFrame();
Correct answer m1b

Hi @Nils M. Barner it's not what you expect!

 

The problem was this line:

fullText = fullText.replace(/\s+/g, " ")

 

In Illustrator (its different in Indesign!) RegExp symbol \s matches a carriage return \r. So if we change it to:

fullText = fullText.replace(/[ \t]+/g, " ")

you are back in business!

- Mark 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
June 28, 2025

Hi @Nils M. Barner it's not what you expect!

 

The problem was this line:

fullText = fullText.replace(/\s+/g, " ")

 

In Illustrator (its different in Indesign!) RegExp symbol \s matches a carriage return \r. So if we change it to:

fullText = fullText.replace(/[ \t]+/g, " ")

you are back in business!

- Mark 

Community Expert
June 28, 2025

holy moly! this completely was out of scope... must have been the sommer heat today... actually I just wanted to get rid of more than one space... probably mixed it up with GREP... thank you so much for pointing it out!

changing it to  fullText = fullText.replace(/ {2,}/g, " ");  fixed it ✅