Question
HELPPP!! SCRIPT THAT ALLOWS ME SPLIT A TEXT PARAGRAPH INTO TWO LINES
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);
