skip to Main Content

I tried to create text with addText(text), but when I click on the execute button nothing happens.

Here are the lines of code

button6.onClick = function() {
    addText();

    function addText() {
        app.beginUndoGroup("My Process");
        var comp = app.project.activeItem;
        var selectedlayer = comp.selectedLayers[0];
        if (comp == null) {
            alert("No composition selected");
        }
        if (comp.selectedLayers[0] == null) {
            alert("Select the layer");
        }
        var outpoint = comp.selectedLayers[0].outPoint;
        var inpoint = comp.selectedLayers[0].inPoint;
        var duration = outpoint - inpoint;
        var textLayer = comp.layers.addText(text);
        var sourceText = textLayer.property("Source Text").value;
        sourceText.fontSize = 100;
        sourceText.font = Arial;
        textLayer.property("Source Text").setValue(sourceText);
        textLayer.adjustmentLayer = false;
        textLayer.inPoint = inpoint;
        textLayer.label = 1;
        comp.layer(1).moveBefore(selectedlayer);
        app.endUndoGroup();
    }
}

I tried to use addBoxText(text), but result was the same

2

Answers


  1. If you are using plain JavaScript, the event function name should be onclick instead of onClick.

    Login or Signup to reply.
  2. It appears that the text variable is not defined in your code snippet, which may be causing the addText() function to fail. You need to pass the desired text as an argument to the addText() function. Here’s how you can make it work:

    button6.onClick = function() {
        var myText = "Hello, After Effects!"; // Replace this with the text you want to add
    
        addText(myText);
    
        function addText(text) {
            app.beginUndoGroup("My Process");
            var comp = app.project.activeItem;
            var selectedlayer = comp.selectedLayers[0];
            if (comp == null) {
                alert("No composition selected");
            }
            if (comp.selectedLayers[0] == null) {
                alert("Select the layer");
            }
            var outpoint = comp.selectedLayers[0].outPoint;
            var inpoint = comp.selectedLayers[0].inPoint;
            var duration = outpoint - inpoint;
            var textLayer = comp.layers.addText(text);
            var sourceText = textLayer.property("Source Text").value;
            sourceText.fontSize = 100;
            sourceText.font = "Arial";
            textLayer.property("Source Text").setValue(sourceText);
            textLayer.adjustmentLayer = false;
            textLayer.inPoint = inpoint;
            textLayer.label = 1;
            comp.layer(1).moveBefore(selectedlayer);
            app.endUndoGroup();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search