Skip to main content
Participating Frequently
October 27, 2023
Question

HELPPP!! SCRIPT THAT ALLOWS ME SPLIT A TEXT PARAGRAPH INTO TWO LINES

  • October 27, 2023
  • 2 replies
  • 1646 views

Hi, I need help with my script. 
- I want it to split the text paragraph layer into 2 lines in separate layers. (It already does it but only works for 1 line, and I want 2)
- Also place the buttons side by side.

If someone can figure out how to do this, please help me re-doing it or explaining me, I would really appreciate it <33 

// Function to add or update text in the composition
function textEditor(thisObj) {
    function updateTextInComp(text) {
        var comp = app.project.activeItem;

        if (comp && comp instanceof CompItem) {
            // Check if a text layer already exists
            var textLayer = null;
            for (var i = 1; i <= comp.layers.length; i++) {
                if (comp.layers[i] instanceof TextLayer) {
                    textLayer = comp.layers[i];
                    break;
                }
            }

            if (textLayer) {
                textLayer.property("Source Text").setValue(text);
            } else {
                textLayer = comp.layers.addText(text);
                textLayer.property("Position").setValue([comp.width / 2, comp.height / 2]);
                textLayer.startTime = comp.time;
            }
        } else {
            alert("Please select or create a composition first.");
        }
    }

    // Function to decompose text into lines
    function decomposeText(text) {
        var comp = app.project.activeItem;

        if (comp && comp instanceof CompItem) {
            // Split the text into lines
            var lines = text.split('\n');

            // Create a text layer for each line
            var yPos = 100; // Initial position for the first line
            for (var i = 0; i < lines.length; i++) {
                var textLayer = comp.layers.addText(lines[i]);
                textLayer.property("Position").setValue([comp.width / 2, yPos]);
                yPos += textLayer.sourceRectAtTime(comp.time, false).height + 10; // Adjust the yPos for the next line
            }
        } else {
            alert("Please select or create a composition first.");
        }
    }

    function textEditor_buildUI(thisObj) {
        var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Text Editor", undefined, {
            resizeable: true
        });
        win.alignChildren = ["fill", "fill"];

        // Title
        var title = win.add("statictext", undefined, "Text Editor");
        title.alignment = "center";
        title.graphics.font = ScriptUI.newFont("Verdana", "BOLD", 16);

        // Text Input
        var textInput = win.add("edittext", undefined, "", {
            multiline: true,
            scrolling: true,
            enterKeySignalsOnChange: true
        });
        textInput.size = [undefined, 200];
        textInput.graphics.backgroundColor = textInput.graphics.newBrush(
            textInput.graphics.BrushType.SOLID_COLOR, [0.2, 0.5, 0.1]
        );
        textInput.graphics.foregroundColor = textInput.graphics.newPen(
            textInput.graphics.PenType.SOLID_COLOR, [0.9, 0.9, 0.9], 1
        );

        // Update Button
        var updateButton = win.add("button", undefined, "Update Text");
        updateButton.alignment = ["center", "bottom"];
        updateButton.onClick = function () {
            var text = textInput.text;
            updateTextInComp(text);
        };

        // Decompose Button
        var decomposeButton = win.add("button", undefined, "Decompose Text");
        decomposeButton.alignment = ["center", "bottom"];
        decomposeButton.onClick = function () {
            var text = textInput.text;
            decomposeText(text);
        };

        win.onResizing = win.onResize = function () {
            this.layout.resize();
        };

        return win;
    }

    var textEditorPal = textEditor_buildUI(thisObj);
    if (textEditorPal != null) {
        if (textEditorPal instanceof Window) {
            textEditorPal.center();
            textEditorPal.show();
        } else {
            textEditorPal.layout.layout(true);
        }
    }
}

textEditor(this);

 

This topic has been closed for replies.

2 replies

Meng Zhiqun
Inspiring
November 3, 2023

I did what you needed and also added some additional stuff.
I added Undogroups, so you don't have to undo multiple times.
I added the ability to select text layers for you to update texts.

Have fun!

// Function to add or update text in the composition
function textEditor(thisObj) {
    function updateTextInComp(text) {
        try{
            var proj = app.project;
            var projComp = proj.activeItem;
            var selLay = projComp.selectedLayers;

            if (projComp && projComp instanceof CompItem) {
                // Check if a text layer already exists

                if(selLay.length==0){
                    var textLayersAry = [];
                    for (var i = 1; i <= projComp.numLayers; i++) {
                        if (projComp.layer(i) instanceof TextLayer) {
                            textLayersAry.push(projComp.layer(i));
                        }
                    }

                    if (textLayersAry.length>0) {
                        for (var tlai = 0; tlai < textLayersAry.length; tlai++) {
                            var curTextLayer = textLayersAry[tlai];
                            curTextLayer.property("Source Text").setValue(text);
                        }
                    } else {
                        var textLayer = projComp.layers.addText(text);
                        textLayer.property("Position").setValue([projComp.width / 2, projComp.height / 2]);
                        textLayer.startTime = projComp.time;
                    }
                }else{
                    for (var slai = 0; slai < selLay.length; slai++) {
                        var curSelLay = selLay[slai];
                        curSelLay.property("Source Text").setValue(text);
                    }
                }
            } else {
                alert("Please select or create a composition first.");
            }
        }catch(e){
            alert(e+e.line);
        }
    }

    // Function to decompose text into lines
    function decomposeText(text) {
        try{
            var comp = app.project.activeItem;

            if (comp && comp instanceof CompItem) {
                // Split the text into lines
                var lines = text.split('\n');

                // Create a text layer for each line
                var yPos = 100; // Initial position for the first line
                for (var i = 0; i < lines.length; i++) {
                    var curLine = lines[i];
                    var textLayer = comp.layers.addText(curLine);
                    textLayer.property("Position").setValue([comp.width / 2, yPos]);
                    yPos += textLayer.transform.anchorPoint.value[1] + 60; // Adjust the yPos for the next line
                }
            } else {
                alert("Please select or create a composition first.");
            }
        }catch(e){
            alert(e+e.line);
        }
    }

    function textEditor_buildUI(thisObj) {
        var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Text Editor", undefined, {
            resizeable: true
        });
        win.alignChildren = ["fill", "fill"];

        // Title
        var title = win.add("statictext", undefined, "Text Editor");
        title.alignment = "center";
        title.graphics.font = ScriptUI.newFont("Verdana", "BOLD", 16);

        // Text Input
        var textInput = win.add("edittext", undefined, "", {
            multiline: true,
            scrolling: true,
            enterKeySignalsOnChange: true
        });
        textInput.size = [undefined, 200];
        textInput.graphics.backgroundColor = textInput.graphics.newBrush(
            textInput.graphics.BrushType.SOLID_COLOR, [0.2, 0.5, 0.1]
        );
        textInput.graphics.foregroundColor = textInput.graphics.newPen(
            textInput.graphics.PenType.SOLID_COLOR, [0.9, 0.9, 0.9], 1
        );

        // Update Button
        var btnGroup = win.add("group", undefined, "btnGroup");

        var updateButton = btnGroup.add("button", undefined, "Update Text");
        updateButton.alignment = ["left", "bottom"];
        updateButton.onClick = function () {
            app.beginUndoGroup("upb");
            var text = textInput.text;
            updateTextInComp(text);
            app.endUndoGroup();
        };

        // Decompose Button
        var decomposeButton = btnGroup.add("button", undefined, "Decompose Text");
        decomposeButton.alignment = ["right", "bottom"];
        decomposeButton.onClick = function () {
            var text = textInput.text;
            app.beginUndoGroup("dct");
            decomposeText(text);
            app.endUndoGroup();
        };

        win.onResizing = win.onResize = function () {
            this.layout.resize();
        };

        return win;
    }

    var textEditorPal = textEditor_buildUI(thisObj);
    if (textEditorPal != null) {
        if (textEditorPal instanceof Window) {
            textEditorPal.center();
            textEditorPal.show();
        } else {
            textEditorPal.layout.layout(true);
        }
    }
}

textEditor(this);

  

Leo BledoAuthor
Participating Frequently
November 3, 2023

Thank you so much for helping. 
Why is it splitting  the text by 1 single line instead of 2? or could you find out how to? 

Meng Zhiqun
Inspiring
November 3, 2023

Sorry I don't exactly understand what are you looking for in this. I thought you wanted to split every paragraph, which the script didn't do. I thought my solution worked for you.

Could you give me a visual reference (something like before/after) of what you are looking for?

Mathias Moehl
Community Expert
Community Expert
October 28, 2023
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects